You are here: Re: generate 2 random numbers in rapid sequence « PHP Programming Language « IT news, forums, messages
Re: generate 2 random numbers in rapid sequence

Posted by Jim Michaels on 04/23/06 04:48

"Domestos" <andy.mak@virgin.netspam> wrote in message
news:1DA2g.22612$LH2.15753@newsfe2-win.ntli.net...
>A less computationally expensive method of accomplishing the same thing is
>to use the mt_rand() function. This function uses the Mersenne Twister
>algorithm and is considerably faster than rand(). In addition as of PHP
>version 4.2.0 there is no need to seed the mt_rand() random number
>generator with srand() or mt_srand()
>
> <?php
>
> // Range of numbers
>
> // Minimum number
> $min = "1";
>
> // Maximum number
> $max = "10";
>
> echo "mt_rand() Mersenne Twister pseudo-random number: ".
> mt_rand($min, $max);
>
> ?>
>

and by the way, I've tried PHP manual's suggested code
<?php
// seed with microseconds
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
mt_srand(make_seed());
$randval = mt_rand();
?>

but it doesn't make any difference. same result.

I've also tried
$q1=mysql_query("SELECT MAX(pid) AS a FROM cpg133_pictures", $link2);
if ($row=mysql_fetch_assoc($q1)) {
time_nanosleep(0, 100000);
mt_srand(make_seed()+$row['a']+rand());
$n=mt_rand(1,$row['a']);


but the 100ms nanosleep (which should affect microtime) didn't do anything
either, neither does the mt_srand. maybe the nanosleep never did anything
because it was too small for execution on UNIX.

I have also tried feeding $n into mt_srand(). between calls, makes no
difference. apparently mt_srand is set by default in PHP by microtime().
so when the interpreter starts up for the 2nd session of my image generator,
I'm stuck.

I had thought I could at least get something decent out of mysql's RAND().

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация