By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

generate 2 random numbers in rapid sequence

P: n/a
I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.
I have not been able to do either. I get the same number back several times
from PHP's mt_rand() and from mysql's RAND().
any ideas?

I suppose I could use the current rancom number as the seed for the next
random number. but would that really work?
Apr 23 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
People who work with computers often talk about their system's "random
number generator" and the "random numbers" it produces. However numbers
calculated by a computer through a deterministic process, cannot, by
definition, be random. Given knowledge of the algorithm used to create the
numbers and its internal state, you can predict all the numbers returned by
subsequent calls to the algorithm, whereas with genuinely random numbers,
knowledge of one number or an arbitrarily long sequence of numbers is of no
use whatsoever in predicting the next number to be generated.

Computer-generated "random" numbers are more properly referred to as
pseudo-random numbers, and pseudo-random sequences of such numbers. A
variety of clever algorithms have been developed which generate sequences of
numbers which pass every statistical test used to distinguish random
sequences from those containing some pattern or internal order.

PHP has a number of inbuilt random functions such as rand(). This function
must be first seeded by srand() in order for it to work properly.

<?php

// Range of numbers

// Minimum number
$min = "1";

// Maximum number
$max = "10";

srand((double) microtime() * 1000003);
// 1000003 is a prime number

echo "rand() pseudo-random number".
rand($min, $max);

?>
Apr 23 '06 #2

P: n/a
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);

?>
Apr 23 '06 #3

P: n/a

"Domestos" <an******@virgin.netspam> wrote in message
news:1D*******************@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);

?>


I know about these. that's why I mentioned them. my problem, as I stated,
is that when I use them in an inline image generator to randomly select an
image from a database, I get the same images on the page. I refresh, I get a
different image because microtime has changed, but both images are again the
same.

This is supposed to be a random image picker. I don't even care if it's
pseudo-random, as long as I get something that looks like a different image
every time.

any ideas?
Apr 23 '06 #4

P: n/a

"Domestos" <an******@virgin.netspam> wrote in message
news:1D*******************@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().
Apr 23 '06 #5

P: n/a
>I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.
Same page hit or different page hits? I cannot explain why you
would get the same number in rapid sequence from several calls on
the same hit.
I have not been able to do either. I get the same number back several times
from PHP's mt_rand() and from mysql's RAND().
any ideas?
When PHP starts up, the seed gets initialized to <SOMETHING>,
possibly based on microtime(), but I really don't care. Apache/PHP
don't get restarted very often. With a good host, it could easily
be less than once a month. However, once PHP starts, it's fixed.

It is likely that if Apache fork()s for a given hit, the seed stays
initialized to <SOMETHING> unless you explicitly set it. Any changes
to that by more calls to get pseudo-random numbers are discarded
when that instance of Apache exits. To get different values, you
need to explicitly set the seed to a function of something other
than just microtime().

Things to use for a seed (jumble them all together, as in concatenate,
then take md5 of result, then convert some of md5 result to integer):
microtime()
getmypid()
$_SERVER['UNIQUE_ID'] (Apache only, and may need a module turned on)
$_SERVER['REMOTE_ADDR']
$_SERVER['REMOTE_PORT']

Actually, using just the first two should be sufficient, as the
pair (microtime(), getmypid()) should be unique. However, to get
more entropy, throw in some of the others.

If you are using pseudo-random numbers to select one of 100 images,
expect the same image twice in a row about 1% of the time, unless
you have code to deal with this. My suggestion is to not attempt
to avoid this.
I suppose I could use the current rancom number as the seed for the next
random number. but would that really work?


It's a very BAD idea to seed a pseudo-random number generator
multiple times in the same run. Using the output of the random
number generator as a new seed is also not a good idea. It's very
easy to cripple a good pseudo-random number generator with a poor
method of choosing a seed. The problem you're having with microtime()
demonstrates this. Seed once. Use a good source of (pseudo-)randomness.
microtime() alone doesn't qualify if you require rapid-fire results.

Gordon L. Burditt
Apr 23 '06 #6

P: n/a

"Gordon Burditt" <go***********@burditt.org> wrote in message
news:12*************@corp.supernews.com...
I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.
Same page hit or different page hits? I cannot explain why you
would get the same number in rapid sequence from several calls on
the same hit.

same page hit on the webserver to a .html file, which has 2 <img
src=img.php> elements.
on a multiprocessor webserver, this may be occurring simultaneously.

I have not been able to do either. I get the same number back several
times
from PHP's mt_rand() and from mysql's RAND().
any ideas?
When PHP starts up, the seed gets initialized to <SOMETHING>,
possibly based on microtime(), but I really don't care. Apache/PHP
don't get restarted very often. With a good host, it could easily
be less than once a month. However, once PHP starts, it's fixed.

It is likely that if Apache fork()s for a given hit, the seed stays
initialized to <SOMETHING> unless you explicitly set it. Any changes
to that by more calls to get pseudo-random numbers are discarded
when that instance of Apache exits. To get different values, you
need to explicitly set the seed to a function of something other
than just microtime().


which makes sense. if all set to microtime, you would still get the same
images.

Things to use for a seed (jumble them all together, as in concatenate,
then take md5 of result, then convert some of md5 result to integer):
microtime()
getmypid()
believe it or not, on these 2 I get the same pictures. frustrating.
$_SERVER['UNIQUE_ID'] (Apache only, and may need a module turned on)
Don't think I have that option. sure sounds good though.
$_SERVER['REMOTE_ADDR']
$_SERVER['REMOTE_PORT']
these two won't make it unique, because both images are going to be all on
the same page.
mt_srand(make_seed()+getmypid()+$_SERVER['UNIQUE_ID']);
$n=mt_rand(1,$row['a']);
tried this, but still doesn't do it. I don't even get an error on
$_SERVER['UNIQUE_ID']. I think it's NULL. is $_SERVER['UNIQUE_ID'] a
string I should hash, or an integer?

Actually, using just the first two should be sufficient, as the
pair (microtime(), getmypid()) should be unique. However, to get
more entropy, throw in some of the others.

If you are using pseudo-random numbers to select one of 100 images,
expect the same image twice in a row about 1% of the time, unless
you have code to deal with this. My suggestion is to not attempt
to avoid this.

I get a different image on next page refresh, possibly due to the time
involved. but the same images all over the page, possibly due to the time
involved.
I suppose I could use the current rancom number as the seed for the next
random number. but would that really work?


It's a very BAD idea to seed a pseudo-random number generator
multiple times in the same run. Using the output of the random
number generator as a new seed is also not a good idea. It's very
easy to cripple a good pseudo-random number generator with a poor
method of choosing a seed. The problem you're having with microtime()


I understand that. I was trying to avoid it. but nothing seems to work
short of having semaphore'd access to a file or database as the seed and
just incrementing it like a counter. And then I'm circumventing the web
server's whole concept of parallelism. :-/
demonstrates this. Seed once. Use a good source of (pseudo-)randomness.
microtime() alone doesn't qualify if you require rapid-fire results.

tell me about it. :-/
Gordon L. Burditt

Apr 23 '06 #7

P: n/a
>> >I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.
Same page hit or different page hits? I cannot explain why you
would get the same number in rapid sequence from several calls on
the same hit.

same page hit on the webserver to a .html file, which has 2 <img
src=img.php> elements.
on a multiprocessor webserver, this may be occurring simultaneously.


Show the code for generating the two <img src= tags. Since PHP is
a procedural and uni-tasking (within any page, unless you start
calling fork()) language, these should NOT be done simultaneously,
even on a multi-processor system, although they might be done fast
enough that microtime() doesn't advance. Also show all the calls to get
random numbers and set seeds.

Are you sure you're not seeding twice with the same seed? SEED ONLY ONCE.

Look at the source HTML code. Are you getting:

- the same image number (e.g. 1) all the time?
- random first image number but the second is always the same as the first?
Things to use for a seed (jumble them all together, as in concatenate,
then take md5 of result, then convert some of md5 result to integer):
microtime()
getmypid()


believe it or not, on these 2 I get the same pictures. frustrating.


Are you running Apache 2.0? It may be using threads instead of
processes, even for PHP. Is PHP even supposed to work with Apache
2.0 yet? On a multiprocessor system?
$_SERVER['UNIQUE_ID'] (Apache only, and may need a module turned on)


Don't think I have that option. sure sounds good though.


The module name is mod_unique_id.
$_SERVER['REMOTE_ADDR']
$_SERVER['REMOTE_PORT']
these two won't make it unique, because both images are going to be all on
the same page.
mt_srand(make_seed()+getmypid()+$_SERVER['UNIQUE_ID']);
$n=mt_rand(1,$row['a']);


Show me the code to generate the SECOND id. You don't repeat both
of those lines of code, do you? SEED ONLY ONCE!
tried this, but still doesn't do it. I don't even get an error on
$_SERVER['UNIQUE_ID']. I think it's NULL. is $_SERVER['UNIQUE_ID'] a
string I should hash, or an integer?


It's a string. Try printing it just to see if it's getting set at all.
If it is getting set, it's probably being interpreted as the integer 0
because you're trying to use it as a number.

Please verify your code:

1) There is at most one call to microtime() in any of the files used
by this page. This call is NOT inside a function. More specifically,
this call is NOT inside make_seed(). Delete that function and
expand it in line (ONCE) if desired.
2) There is at most one call to any function to set a seed (srand,
mt_srand). This call is NOT inside a function.

Gordon L. Burditt
Apr 23 '06 #8

P: n/a

"Gordon Burditt" <go***********@burditt.org> wrote in message
news:12*************@corp.supernews.com...
I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.


Same page hit or different page hits? I cannot explain why you
would get the same number in rapid sequence from several calls on
the same hit.
I have not been able to do either. I get the same number back several
times
from PHP's mt_rand() and from mysql's RAND().
any ideas?


When PHP starts up, the seed gets initialized to <SOMETHING>,
possibly based on microtime(), but I really don't care. Apache/PHP
don't get restarted very often. With a good host, it could easily
be less than once a month. However, once PHP starts, it's fixed.

It is likely that if Apache fork()s for a given hit, the seed stays
initialized to <SOMETHING> unless you explicitly set it. Any changes
to that by more calls to get pseudo-random numbers are discarded
when that instance of Apache exits. To get different values, you
need to explicitly set the seed to a function of something other
than just microtime().

Things to use for a seed (jumble them all together, as in concatenate,
then take md5 of result, then convert some of md5 result to integer):
microtime()
getmypid()
$_SERVER['UNIQUE_ID'] (Apache only, and may need a module turned on)
$_SERVER['REMOTE_ADDR']
$_SERVER['REMOTE_PORT']

Actually, using just the first two should be sufficient, as the
pair (microtime(), getmypid()) should be unique. However, to get
more entropy, throw in some of the others.

If you are using pseudo-random numbers to select one of 100 images,
expect the same image twice in a row about 1% of the time, unless
you have code to deal with this. My suggestion is to not attempt
to avoid this.
I suppose I could use the current rancom number as the seed for the next
random number. but would that really work?


It's a very BAD idea to seed a pseudo-random number generator
multiple times in the same run. Using the output of the random
number generator as a new seed is also not a good idea. It's very
easy to cripple a good pseudo-random number generator with a poor
method of choosing a seed. The problem you're having with microtime()
demonstrates this. Seed once. Use a good source of (pseudo-)randomness.
microtime() alone doesn't qualify if you require rapid-fire results.

Gordon L. Burditt


well, I have one last thing I can do. I asked about doing XML inline images
and got this really cook info. hope it would work with HTML.
echo "<img src=\"data:$mimetype;base64," .
base64_encode(file_get_contents($filepath)) . "\" alt=\"$alt\" width=\"$w\"
height=\"$h\" style=\"$style\"$end>";

just insert this into a function. from http://www.ietf.org/rfc/rfc2397.txt .
calling a function to generate an imline image should allow me to use the
random number generator as it's supposed to be used. then I don't have to
use <img src="img.php">

just tried it. output looks correct, but doesn't show an image. :-(
and base64_encode() sure is slow!
Alas, I found out this only works in NS, not IE. IE requires MIME encoding,
which I've never done, and since it looks like it requires headers, I may be
back to square one problem with the seed.
Apr 23 '06 #9

P: n/a
Message-ID: <rf******************************@comcast.com> from Jim
Michaels contained the following:
well, I have one last thing I can do.


More than that. I've used rand() loads of times and not had a problem.

See:http://www.ckdog.co.uk/phpcourse/DICE/DICEGAME.PHP which picks two
random numbers and displays images according to what the number is.

With the code

$num=rand(1,6);
$num2=rand(1,6);
$total=$num+$num2;

$num1 and $num2 are only occasionally the same (as you might expect)
Full code here:-

http://www.ckdog.co.uk/phpcourse/DICE/dicegame.phps
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Apr 23 '06 #10

P: n/a

"Gordon Burditt" <go***********@burditt.org> wrote in message
news:12*************@corp.supernews.com...
>I need to generate 2 random numbers in rapid sequence from either PHP
>or
mysql.

Same page hit or different page hits? I cannot explain why you
would get the same number in rapid sequence from several calls on
the same hit.

same page hit on the webserver to a .html file, which has 2 <img
src=img.php> elements.
on a multiprocessor webserver, this may be occurring simultaneously.


this is essentially what I had before I switched over to something that I
found out later only works on NS :-/

<?php
function makeThumbnail($o_file, /*$t_ht = 150*/$t_wd=150) {
$image_info = getimagesize($o_file); // see EXIF for faster way

switch ($image_info['mime']) {
case 'image/gif':
if (imagetypes() & IMG_GIF) { // not the same as IMAGETYPE
$o_im = imagecreatefromgif($o_file) ;
} else {
$ermsg = 'GIF images are not supported<br />';
}
break;
case 'image/jpeg':
if (imagetypes() & IMG_JPG) {
$o_im = imagecreatefromjpeg($o_file) ;
} else {
$ermsg = 'JPEG images are not supported<br />';
}
break;
case 'image/png':
if (imagetypes() & IMG_PNG) {
$o_im = imagecreatefrompng($o_file) ;
} else {
$ermsg = 'PNG images are not supported<br />';
}
break;
case 'image/wbmp':
if (imagetypes() & IMG_WBMP) {
$o_im = imagecreatefromwbmp($o_file) ;
} else {
$ermsg = 'WBMP images are not supported<br />';
}
break;
default:
$ermsg = $image_info['mime'].' images are not supported<br />';
break;
}

if (!isset($ermsg)) {
$o_wd = imagesx($o_im) ;
$o_ht = imagesy($o_im) ;
// thumbnail width = target * original width / original height
//$t_wd = round($o_wd * $t_ht / $o_ht) ;
$t_ht = round($o_ht * $t_wd / $o_wd);
$t_im = imagecreatetruecolor($t_wd,$t_ht);

imagecopyresampled($t_im, $o_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd,
$o_ht);

imagejpeg($t_im); //output to browser

imagedestroy($o_im);
imagedestroy($t_im);
}
return isset($ermsg)?$ermsg:NULL;
}

function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}

//get next row from pictures table since we can't do random
$q2=mysql_query("SELECT pictures.pid AS pid
FROM pictures,idx
WHERE pictures.pid>idx.pid
LIMIT 1", $link2);
if ($row2=mysql_fetch_assoc($q2)) {
mysql_query("UPDATE idx SET pid=$row2[pid]", $link2);
$n=$row2['pid'];
} else { //reached end of table. wrap to beginning.
mysql_query("UPDATE idx SET pid=1", $link2);
$n=1;
}
mysql_free_result($q2);

//try random anyway. overwrite results of "nextrow".
$q3=mysql_query("SELECT MAX(pid) AS a FROM cpg133_pictures", $link2);
if ($row=mysql_fetch_assoc($q3)) {
//tried commenting out the line below. no difference.
mt_srand(make_seed()+getmypid()/*+$_SERVER['UNIQUE_ID']*/);
$n=mt_rand(1,$row['a']);
//$n=rand(1,$row['a']); //for some reason, causes code to break
}
mysql_free_result($q3);
$result = mysql_query("SELECT filepath,filename,pwidth,pheight FROM pictures
WHERE pid=$n", $link2);
if ($row = mysql_fetch_assoc($result)) {
//determine mime type from extension on filename
$ext=strrchr($row['filename'],'.');
switch($ext){
case '.jp2':
case '.jpg':
case '.jpeg':
default: $mimetype='image/jpeg';break;
case '.gif': $mimetype='image/gif';break;
case '.png': $mimetype='image/x-png';break;
}
$fname=$_SERVER['DOCUMENT_ROOT'] . '/pix/' . $row['filepath'] .
$row['filename'];
}
mysql_free_result($result);
mysql_close($link2);

if (isset($_GET['width'])) {
makeThumbnail($fname, $_GET['width']);
} else {
makeThumbnail($fname);
}
?>
//example: <img src="img.php?width=150" width=150>

Show the code for generating the two <img src= tags. Since PHP is
a procedural and uni-tasking (within any page, unless you start
calling fork()) language, these should NOT be done simultaneously,
even on a multi-processor system, although they might be done fast
enough that microtime() doesn't advance. Also show all the calls to get
random numbers and set seeds.

IE requests/loads images in series-parallel very rapidly. Have you ever
watched it? That's where it's coming from I think.

Are you sure you're not seeding twice with the same seed? SEED ONLY ONCE.

Look at the source HTML code. Are you getting:

- the same image number (e.g. 1) all the time?
- random first image number but the second is always the same as the
first?
Things to use for a seed (jumble them all together, as in concatenate,
then take md5 of result, then convert some of md5 result to integer):
microtime()
getmypid()


believe it or not, on these 2 I get the same pictures. frustrating.


Are you running Apache 2.0? It may be using threads instead of
processes, even for PHP. Is PHP even supposed to work with Apache
2.0 yet? On a multiprocessor system?
$_SERVER['UNIQUE_ID'] (Apache only, and may need a module turned on)


Don't think I have that option. sure sounds good though.


The module name is mod_unique_id.
$_SERVER['REMOTE_ADDR']
$_SERVER['REMOTE_PORT']


these two won't make it unique, because both images are going to be all on
the same page.
mt_srand(make_seed()+getmypid()+$_SERVER['UNIQUE_ID']);
$n=mt_rand(1,$row['a']);


Show me the code to generate the SECOND id. You don't repeat both
of those lines of code, do you? SEED ONLY ONCE!
tried this, but still doesn't do it. I don't even get an error on
$_SERVER['UNIQUE_ID']. I think it's NULL. is $_SERVER['UNIQUE_ID'] a
string I should hash, or an integer?


It's a string. Try printing it just to see if it's getting set at all.
If it is getting set, it's probably being interpreted as the integer 0
because you're trying to use it as a number.

Please verify your code:

1) There is at most one call to microtime() in any of the files used
by this page. This call is NOT inside a function. More specifically,
this call is NOT inside make_seed(). Delete that function and
expand it in line (ONCE) if desired.
2) There is at most one call to any function to set a seed (srand,
mt_srand). This call is NOT inside a function.

Gordon L. Burditt

Apr 23 '06 #11

P: n/a
Well, don't seed based on time. Use getmypid() as part of the seeding.
Perhaps you could get the two processes aware of each other, or aware
of the last choice made.

Apr 24 '06 #12

P: n/a

"fletch" <ri****************@googlemail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Well, don't seed based on time. Use getmypid() as part of the seeding.
Perhaps you could get the two processes aware of each other, or aware
of the last choice made.


found out that the database is an excellent source of random numbers if you
are stuck doing <img src=img.php...>

Also discovered that my host's apache configuration might be load PHP as an
..SO module under UNIX, therefore PHP *might* not get its own PID. If UNIX
..SO's are anything like Windows DLL's, only Apache has the PID - PHP
wouldn't even be a child process - it is simply allocated a chunk of memory
for its functions to load in.

If this were a CGI, then it would definitely get its own PID, but PHP.net
doesn't recommend this method of configuration.

To be honest, I haven't tried getmypid() after discovering that the URL on
src= needs to be unique so that the image isn't cached and therefore
duplicated. Now that I've got everything working, maybe I'll give it one
more go just to find out.
Apr 25 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.