GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   php question, best way for random numbers? (https://gfy.com/showthread.php?t=893913)

fris 03-16-2009 05:20 AM

php question, best way for random numbers?
 
Whats the best way for random number generation?

this is what im using now

Code:

<?
mt_srand ((double) microtime() * 1000000);
$random = mt_rand(1,100);
echo $random;
?>


HorseShit 03-16-2009 05:21 AM

not sure but im sure our channel coders will know :)

grumpy 03-16-2009 05:29 AM

you dont need the mt_srand ((double) microtime() * 1000000);
Its build in in the mt_rand

Libertine 03-16-2009 05:45 AM

Quote:

Originally Posted by grumpy (Post 15634478)
you dont need the mt_srand ((double) microtime() * 1000000);
Its build in in the mt_rand

Yep. As of 4.2, seeding is no longer needed, if I recall correctly.

mt_rand() will do for most purposes, but if you need pseudo-random data for purposes related to cryptography, keep in mind that mt_rand uses a mersenne twister, which is rather weak. So, in those cases, getting your pseudo-random data from /dev/urandom is a better option.

leek 03-16-2009 05:47 AM

Quote:

Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
RTFM :thumbsup

Brujah 03-16-2009 05:50 AM

I believe that you don't really need to use mt_srand with php 5.2.1+
You could use uniqid as a seed, or data from /dev/random

Brujah 03-16-2009 05:51 AM

Quote:

Originally Posted by grumpy (Post 15634478)
you dont need the mt_srand ((double) microtime() * 1000000);
Its build in in the mt_rand

True, but there was a fuss about it being insecure until 5.2.1+ I think

fris 03-16-2009 06:30 AM

im just getting a random number for random image headers, have 10 headers, want to rotate through them

Libertine 03-16-2009 06:53 AM

Quote:

Originally Posted by fris (Post 15634617)
im just getting a random number for random image headers, have 10 headers, want to rotate through them

mt_rand will do fine, then.

The other stuff only comes into play when dealing with things where security is an issue, like when a forgotten admin password of a site gets reset on request (which actually was a security hole in Joomla and WP).

yuu.design 03-16-2009 07:49 AM

Quote:

Originally Posted by fris (Post 15634617)
im just getting a random number for random image headers, have 10 headers, want to rotate through them

<?php
$image = rand(1,10);

echo "<img src=\"$image.jpg\" width=\"\" height=\"\" alt=\"\" />";

?>

:thumbsup

Killswitch - BANNED FOR LIFE 03-16-2009 08:49 AM

Like above, rand() will work fine for what you need.


All times are GMT -7. The time now is 10:26 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123