• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

PHP: mt_rand()

vern

Dominus
Political User
#1
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?
 
#2
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
 
#3
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 :)
 

vern

Dominus
Political User
#5
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.
 

X-Istence

*
Political User
#6
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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,962
Messages
673,240
Members
89,015
Latest member
loxioalix