|
|
 |
(PHP 3>= 3.0.6, PHP 4 ) mt_rand --
Génère une meilleure valeur aléatoire.
Descriptionint mt_rand ( int min, int max)
De nombreux générateurs de nombres aléatoires
provenant de vieilles bibliothèques libcs ont des comportement
douteux et sont très lents. Par défaut, PHP utilise le
générateur de nombres aléatoires de libc avec la
fonction rand(). mt_rand() est une
fonction de remplacement, pour cette dernière. Elle utilise un
générateur de nombres aléatoire de caractéristique
connue, le " Mersenne Twister ", qui va produire des nombres utilisables en
cryptographie, et qui est 4 fois plus rapide que la fonction standard libc.
La "Homepage of the Mersenne Twister " est
http://www.math.keio.ac.jp/~matumoto/emt.html. Une version optimisée
des sources de MT est disponible à
http://www.scp.syr.edu/~marc/hawk/twister.html.
Appelée sans les arguments optionnels min et
max, mt_rand() retourne un nombre
pseudo-aléatoire, entre 0 et RAND_MAX.
Pour obtenir un nombre entre 5 et 15 inclus, il faut utiliser
mt_rand(5,15).
N'oubliez pas d'initialiser le générateur de nombres
aléatoires avec mt_srand().
Note :
Dans les versions antérieures à la 3.0.7, la signification
du paramètre max était "longueur".
Pour avoir le même résultat, il faut utiliser
mt_rand (5, 11) pour obtenir un nombre
aléatoire entre 5 et 15.
Voir aussi
mt_srand(),
mt_getrandmax(),
srand(),
rand() et
getrandmax().
User Contributed Notes mt_rand |
 |
mrdlinux@yahoo.com
21-Jul-2000 04:02 |
|
And for those who prefer scaling:
mt_rand() / RAND_MAX * (Max - Min) + Min;
|
|
gerwout@minso.nl
27-Mar-2001 02:29 |
|
Don't forget to do mt_srand() before the mtrand() or you wil get the same
values every time you'll go back to the same page. Not when you refresh
the page, only when you open the page in a new browserwindow and the
history is empty.
|
|
amcclung@stetson.edu
18-Apr-2001 07:27 |
|
Here's an elegant way of generating a random float value within a certain
range:
$range = $upperBound-$lowerBound;
$num = $lowerBound + $range * mt_rand(0, 32767)/32767;
You should now have a floating point number between your $lowerBound (i.e.
0.5) and $upperBound (0.75) values.
|
|
|
12-Jan-2002 04:33 |
|
There is a intresting article on Developers resources about
Pseudo-random PHP functions and truly random number generators.
http://www.developers-resources.com/stories.php?story=01/12/03/3988084
They have wrote a function that randomly retrieves a random number. Either
generated by the computer pseudo-randomly or from one of two truly random
number sites.
|
|
psychoborg@yahoo.com
29-Jan-2002 04:43 |
|
well, the rand function works, but looks like it has a limitation to it.
look at this :
function calcres($number)
{
global $Supplies;
srand ((double) microtime() * 1000000);
$retval = 0;
$a = 0;
if ($a!=$number)
{
do
{
$a++;
$tmp = rand(0,3+($roids+$retval));
if ($tmp<20) $retval++;
}while($a<$number);
}
return $retval;
}
Later it takes this random number and adds it to the data base fine. adding
to the random numbers up until you hit a specific point.
now, while this works fine, when you hit "32780" or around there
in the data base(in this case mySQL) you then get errors showing that the
line is out of range. like the following:
Warning: rand(): Invalid range: 0..32780 in ..usr\inet\waves.php on line
20
What can be done to overcome this possible limitation?
My question is, would there be a workaround or fix to this limitation? or
is this a limitation of the rand() function? and not a bug in this
programming structure.
|
|
psychoborg@yahoo.com
31-Jan-2002 06:34 |
|
ok, an update to my last comment
well, the rand function works, but looks like it has a limitation to it.
look at this :
function calcres($number)
{
global $Supplies;
srand ((double) microtime() * 1000000);
$retval = 0;
$a = 0;
if ($a!=$number)
{
do
{
$a++;
# $tmp = rand(0,3+
# the rand is limited in the function, Especcially with the
current formula that im using here. but the fix is quite simple. just
change the rand to mt_rand this will increase your random findings and
stablize your php program,
($supplies+$retval));
if ($tmp<20) $retval++;
}while($a<$number);
}
return $retval;
}
This function will work best with the mt_rand runction and not only that,
it is most defanitly more stable. meaning you will not hit the limitation
that will cause the SQL event out of range error.
|
|
demogracia@metropoliglobal.com
02-Mar-2002 06:38 |
|
//
// Generates a random string with the specified length
// Chars are chosen from the provided [optional] list
//
function simpleRandString($length=16,
$list="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"){
mt_srand((double)microtime()*1000000);
$newstring="";
if($length>0){
while(strlen($newstring)<$length){
$newstring.=$list[mt_rand(0, strlen($list)-1)];
}
}
return $newstring;
}
//
// Generates a random string with the specified length
// Includes: a-z, A-Z y 0-9
//
function randString($length=16){
mt_srand((double)microtime()*1000000);
$newstring="";
if($length>0){
while(strlen($newstring)<$length){
switch(mt_rand(1,3)){
case 1: $newstring.=chr(mt_rand(48,57)); break; // 0-9
case 2: $newstring.=chr(mt_rand(65,90)); break; // A-Z
case 3: $newstring.=chr(mt_rand(97,122)); break; // a-z
}
}
}
return $newstring;
}
|
|
guywhous@esphp.com
11-Mar-2002 01:09 |
|
On an Apache 1.3.3 server, the seed mt_rand() uses resets to the same value
every time the PHP file is uploaded. This means that if mt_rand(1,500)
returns 273 the first time it is used in your PHP file, it will return 273
again if you re-upload this file. You have been warned.
|
|
tom@globalmedia.org
01-May-2002 06:24 |
|
To return a random decimal value between 0 and 1:
$randval=mt_rand()/mt_getrandmax();
|
|
 |
 | |