http://www.icyphoenix.com/viewtopic.php?f=28&t=723&p=5964#p5964
-----------------------------------
Xusqui
Mon 18 Dec, 2006 22:01

Help on Dice BBCode!
-----------------------------------
Hi, neighbours and friends...

I'm trying to make a new bbcode for Role Playing Games and whatever a random number generation could be for.

I used to use it for making contests in my web.

I've made this:

open includes/bbcode.php
Find (for instances)
[code]			'fade' => array(
					'nested' => true,
					'inurl' => true,
					'allow_empty' => false,
					),[/code]

Right after add
[code]			'dice' => array(
					'nested'	=> false,
					'inurl'		=> false,
					'allow_empty'	=> false,
					),[/code]

Find
[code]// --- Disable Active Content Switch BEGIN ---[/code]

Right before add
[code]		// BBcode dice, by Roland at ka-tet-corp.com. Based upon MOD-dice by >achary Aerogos (tribalgenesis at hotmail.com)
		if($tag === 'dice')
		{
			$Die = $content;
			$Expressions = explode('d', $content); 
			if( (isset($Expressions[0])) && (isset($Expressions[1])) ) 
			{ 
				$dice_Rolls = $Expressions[0]; 
				$MT_Seed = (double)microtime()*1000000;//intval($Expressions[1]);
			} 
			$Die_Count = $Expressions[0];
			$Die_Type = $Expressions[1];
			// Make sure we restore the MT gen to a random state after we are done... 
			$Future_Seed = mt_rand(); 
			mt_srand( $MT_Seed ); 

			$Original_Roll_String = '(' . $content . ') <b><span style="color: #AA0000"> Fixed</span></b>'; 
			$Die_Rolls = explode(' ', trim($dice_Rolls)); 
			$header = '<b>Sort of roll</b>: ' . $Die_Count . ' dices of ' . $Die_Type . ' sides ' . $Original_Roll_String . '<br /><b>' . $Die_Type . '-Result: </b>'; 
			$footer = ''; 
			$Die_Count = intval($Die_Count); 
			if( $Die_Count == 0 ) $Die_Count = 1; 
			$total = 0; 
			
			// Loop Limit to prevent 500000d500000 sort of dices due to max. execution time limit 
			if($Die_Count <= 200 && $Die_Type <= 100) 
			{ 
				for( $i = 0; $i < $Die_Count; $i++ ) 
				{ 
					if( $Die_Type == 100 ) 
					{ 
						$value1 = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * 10) * 10; 
						$value2 = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * 10); 
						$total = $total + ($value1 + $value2); 
						$footer = ($i != $Die_Count - 1) ? $footer . $value1 . '+' . $value2 . '(' .($value1 + $value2) . '), ' : $footer .  $value1 . '+' . $value2 . '(' . ($value1 + $value2) . ')'; 
					} 
					else 
					{ 
						$value = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * $Die_Type) + 1; 
						$total = $total + $value; 
						$footer = ($i != $Die_Count - 1) ? $footer . $value . ', ' : $footer . $value . ''; 
					} 
				} 
			} // Loop limit 
			else 
			{ 
				$total = 0; 
				$footer = 'Too many dices and/or too many sides'; 
			} 
				// I do b - /b on purpose...  kills out some smilies that crop up. 
			if( ($Die_Count > 1)) 
			{ 
				$footer=  $footer . ' (<b>Total =</b> ' . $total . '<b></b>)<BR>'; 
			} 
			else 
			{ 
				$footer =  $footer . '<BR>'; 
			} 
			$footer = $footer. ""; 
			$results = $header . $footer; 

			// Restore MT randomness 
			mt_srand($Future_Seed); 
			 
			//list($usec,$sec)=explode(" ",microtime()); 
			mt_srand((double)microtime()*1000000);//$sec * $usec); 
			 
			// Loop to fix randomness problem of results when multiple die rolls are made in a single post - Hades 
				mt_rand(); 

			return array(
				'valid'  => true,
				'html'  => $results
				);
		}[/code]

To use it, you write:
[code][dice]xdyy[/dice][/code]

x means number of dices to roll
yy means number of sides of each dice

i.e
[code][dice]2d10[/dice][/code]
To roll 2 dices of 10 sides.

But now comes the problem.

Because of this line:
[code]$MT_Seed = (double)microtime()*1000000;[/code]

Everytime the post is reloaded, the result changes.

I fixed it using precompiled posts, so the message is saved as is and the dices are not rolled every time the post is reloaded.

But I would like to know what should I do to avoid the precompiled posts.

I've tried this
[code]$MT_Seed = $uid;[/code]
to use a random number that won't change everytime the post is loaded, but, as $uid is null, I always get the same "Random" number. I mean, If I roll 100 times a dice like 1d10 in 100 different posts, I always get the same number... That's not too random, isn't it?

Any idea is welcome!

Thanks in advance!!


