note 23002 added to function.rand

From: Date: Sat, 06 Jul 2002 12:45:01 +0000
Subject: note 23002 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-32676@lists.php.net to get a copy of this message
Hi all , In response to neotzc@hotmail.com for generating random number on Windows outta [-32767,32767]. I have a little tip. Imagine that you want to generate random numbers in this interval [-1000000, 1000000]. You can simply generate a number with rand() function first. After you divide 1000000 by the value you get from rand(). That's all. I have written a little function who performs the mapping of the interval [-32767,32767] with any interval like the [RANGE_MIN, RANGE_MAX]. function rand_map($value_from_rand,$RANGE_MIN,$RANGE_MAX) { $b=($RANGE_MAX-$RANGE_MIN)/2; $a=($RANGE_MAX-$b)/32767; return $a*$value_from_rand+$b; } /* Example of use */ $val=rand(-32767,32767); $val=rand_map( $val, -500000, 40000); // do something with $val ... HTH, BigGun -- http://www.php.net/manual/en/function.rand.php http://master.php.net/manage/user-notes.php?action=edit+23002 http://master.php.net/manage/user-notes.php?action=delete+23002 http://master.php.net/manage/user-notes.php?action=reject+23002

« previous php.notes (#32676) next »