Re: generate 2 random numbers in rapid sequence




"Gordon Burditt" <gordonb.a2nrk@xxxxxxxxxxx> wrote in message
news:124mac1o4mf8f6d@xxxxxxxxxxxxxxxxxxxxx
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


.



Relevant Pages

  • Re: generate 2 random numbers in rapid sequence
    ... Same page hit or different page hits? ... enough that microtime() doesn't advance. ... random numbers and set seeds. ... processes, even for PHP. ...
    (comp.lang.php)
  • Re: What do designers know about Pantone colors
    ... It murders the 4C process images and there's a better way. ... And the statement about black going down after metalics I just can't ... black down first, spread, and hit the metalic heavy on top. ... I'd say you're right on in your approach to metallics. ...
    (comp.publish.prepress)
  • Re: What do designers know about Pantone colors
    ... It murders the 4C process images and there's a better way. ... And the statement about black going down after metalics I just can't ... One has to evaluate the design and determine the best approach. ... black down first, spread, and hit the metalic heavy on top. ...
    (comp.publish.prepress)
  • Re: generate 2 random numbers in rapid sequence
    ... Same page hit or different page hits? ... possibly based on microtime(), ... these two won't make it unique, because both images are going to be all on ... If you are using pseudo-random numbers to select one of 100 images, ...
    (comp.lang.php)
  • Re: A .net 2002 Error
    ... > run with no images in control No Error. ... > After putting 1 Image in control and not even using it When I hit run I ... If you are using a manifest file with the "devenv.exe" ... file and recreate the forms containing the imagelists. ...
    (microsoft.public.dotnet.languages.vb)