less flawed random number generator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

less flawed random number generator

Post by jasongr »

Hello

I have read that the rand() function is quite flawed in the "random" numbers it is generating. I know that I can call srand like so:

Code: Select all

srand(make_seed());
before invoking rand(), and in fact as of PHP 4.2.0, there is no need to seed the random number generator as this is now done automatically.

Does anyone know of a better random number generator that I can use for PHP?

Jason
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

What about writing your own random generator ;-)
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

rand() is not very random.
Basically number i has some correllation to number i-1.
If I use rand() to generate a very long list of numbers there is a fair probability that the distribution of the numbers won't be uniform.

True random genrators are implemented in hardware.
I am looking for an improvement for the rand() function if anyone knows of one
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]mt_rand[/php_man]
Post Reply