note 23002 deleted from function.rand by aidan

From: Date: Sat, 10 Jul 2004 02:33:33 +0000
Subject: note 23002 deleted from function.rand by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72961@lists.php.net to get a copy of this message
Note Submitter: fbiggun@hotmail.com ---- 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

« previous php.notes (#72961) next »