PHP: mt_rand()

vern

Dominus
Political Access
Joined
2 Mar 2002
Messages
1,572
This is probably more of a math question than anything else, but does anyone know of the probability of mt_rand() coming up with two identical values? Note that I am using mt_rand to generate a unique identifier for an item, and store that in a database. As the number of items grows, the probability grows, but how long before this becomes a problem?
 
mt_rand() uses a Mersenne Twister which is supposed to result in a better random value, of course there are always chances of a collision with any random function, but if you seed the random number generator with mt_srand() with something which changes fairly periodically such as microtime() you might reduce the chances of a collision
 
Depending on the size of the database, you could first generate a number, check it for existence in the db and if it exists, generate another number. I have done this on a website I am currently (almost finished) programming for a pizza website, and it's working quite well. Unless you have a db with 1000's of entries, it could be slow I guess. But that all depends on your hardware setup :)
 
why not just start with a number then keep adding one to it for each new item
 
The database I'm talking about is fairly large and heavily used. ~211,000 records right now. I don't really want to start querying the db twice everytime a new entry is added.

@Khayman: the database is already there, so no, I can't start from scratch and have the field auto-increment.

Will have to look for some papers on this I guess as I'm no mathematician.
 
seed it properly, if ona unix box, pipe /dev/random into the seeder, that should be good enough, but there is no gaurantee. Best bet is two queries, or one, and see if that fails, and if it does, then fix it.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back