LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   question about seeding mt_rand in php (https://www.linuxquestions.org/questions/programming-9/question-about-seeding-mt_rand-in-php-718024/)

frieza 04-09-2009 03:20 PM

question about seeding mt_rand in php
 
here's a simple script i wrote using gdlib rendering to create a random forum sig for other forums (no img tags in this forum
Code:

<?php
$me = mt_rand(1,78);
$pic=imagecreatefromjpeg($me.".jpg");
list($width, $height, $type, $attr) = getimagesize($me.".jpg");
$layer = imagecreatetruecolor  ( 250  , 250  );
$blue = imagecolorallocate  ( $layer  , 0  , 0  , 0  );
imagefill($layer,0,0,$blue);
$white = imagecolorallocate  ( $layer  , 204  , 255  , 255  );
imagecopyresampled  ( $layer  , $pic  , 0  , 35  , 0  , 0  , 250  , 215  , $width  , $height  );
imagefttext($layer,25,0,60,30,$white,"./Norasi-Oblique.ttf","HOPPER" );
header("Content-type: image/jpeg");
imagejpeg($layer);
imagedestroy($layer);
imagedestroy($pic);
?>

granted i know seeding the random number generator is no longer explicitly necessary, but how could i seed the random number to decrease the likelyhood of duplicates when this script is called multiple times from the same page?

Hko 04-09-2009 05:52 PM

Quote:

Originally Posted by frieza (Post 3503896)
i know seeding the random number generator is no longer explicitly necessary, but how could i seed the random number to decrease the likelyhood of duplicates when this script is called multiple times from the same page?

If the PHP docs say it is not needed, it is probably the best not to do it. If you would seed the readnom generator every time you script is called from some web page, the random numbers would becom much more predictable.

If you do it, the important thing is to make sure it is done once.

frieza 04-09-2009 08:52 PM

fair enough, any ideas perhaps on how to reduce the chance of getting duplicates when being displayed say.. 9 times?

frieza 04-09-2009 09:02 PM

figured it out i think using setcookie
nvm it didnt work

frieza 04-10-2009 01:14 AM

how does this page appear to you?
http://hoppernet.rubiesofeventide.org/testpage.html

Hko 04-10-2009 03:40 AM

Quote:

Originally Posted by frieza (Post 3504323)
how does this page appear to you?
http://hoppernet.rubiesofeventide.org/testpage.html

9 times the same picture.
But every page load it is a different picture.

Is that what you intended? Or did you want 9 different pictures each time?

frieza 04-10-2009 08:40 PM

i was hoping for 9 pictures each time.. which is funny because that's what i see


All times are GMT -5. The time now is 06:21 PM.