A random number in the range 1-100: <?php InsertRandom(100); ?>.InsertRandom( range )
<?php //----------------------------------------------------- // InsertRandom($range) // - - - - - - - - - - // Print the value of a new random variable in the // range: 1 - $range. // // See Randomize(). //----------------------------------------------------- function InsertRandom($range) { print Randomize($range); } ?>Randomize( range )
<?php //----------------------------------------------------- // Randomize($range) // - - - - - - - - - // Set the global variables for a new random variable // in the range: 1 - $range. // // Returns the value of the new random variable. // // $GLOBALS[RandomVar] - contains the value for the // current random variable. // $GLOBALS[RandomThresh] - contains the sum of the // thresholds called using // Show/HideSameRand(). //----------------------------------------------------- function Randomize( $range ) { if(empty($GLOBALS[RandomVar])) mt_srand(time()); $GLOBALS[RandomThresh] = 0; return $GLOBALS[RandomVar] = mt_rand( 1, $range ); } ?>
<?php if( ShowRandom(2,4) ) { ?>
A random number between one and four was chosen.
This is displayed if the number is one or two.
<?php } ?>
ShowRandom( threshold, range )
<?php
//-----------------------------------------------------
// ShowRandom($threshold,$range)
// - - - - - - - - - - - - - - -
// Set the random varible to a random number in the
// range: 1 - $range.
//
// Returns true if the random variable is less than or
// equal to $threshold.
//-----------------------------------------------------
function ShowRandom( $threshold, $range )
{
return( Randomize($range) <= ($GLOBALS[RandomThresh] = $threshold) );
}
?>
<?php if( HideRandom(1,3) ) { ?>
A random number between one and three was chosen.
This is hidden one third of the time.
<?php } ?>
HideRandom( threshold, range )
<?php
//-----------------------------------------------------
// HideRandom($threshold,$range)
// - - - - - - - - - - - - - - -
// Set the random varible to a random number in the
// range: 1 - $range.
//
// Returns false if the random variable is less than or
// equal to $threshold.
//-----------------------------------------------------
function HideRandom( $threshold, $range )
{
return !ShowRandom($threshold, $range);
}
?>
The last random number generated was <?php InsertSamerand(); ?>.InsertSamerand()
<?php //----------------------------------------------------- // InsertSamerand() // - - - - - - - - // Print the value of the current random variable. //----------------------------------------------------- function InsertSamerand() { print $GLOBALS[RandomVar]; } ?>
<?php if( ShowRandom(1,4) ) { ?>
You are in the lower 25%.
<?php } ?>
<?php if( ShowSamerand(2) ) { ?>
You are in the middle 50%.
<?php } ?>
<?php if( ShowSamerand(1) ) { ?>
You are in the upper 25%.
<?php } ?>
ShowSamerand( threshold )
<?php
//-----------------------------------------------------
// ShowSamerand($threshold)
// - - - - - - - - - - - -
// Returns true if the random variable is greater than
// the sum of the previous thresholds and less than
// or equal to the sum plus $threshold.
//-----------------------------------------------------
function ShowSamerand( $threshold )
{
return ( $GLOBALS[RandomThresh] < $GLOBALS[RandomVar] &&
$GLOBALS[RandomVar] <= ($GLOBALS[RandomThresh]+=$threshold) );
}
?>
<?php if( HideRandom(20,60) ) { ?>
You did not make the lower third.
<?php } ?>
<?php if( HideSamerand(20) ) { ?>
You did not make the middle third.
<?php } ?>
<?php if( HideSamerand(20) ) { ?>
You did not make the upper third.
<?php } ?>
HideSamerand( threshold )
<?php
//-----------------------------------------------------
// HideSamerand($threshold)
// - - - - - - - - - - - -
// Returns false if the random variable is greater
// than the sum of the previous thresholds and less
// than or equal to the sum plus $threshold.
//-----------------------------------------------------
function HideSamerand( $threshold )
{
return !ShowSamerand($threshold);
}
?>