Home What is the max number mt_rand() can generate in windows?
Reply: 1

What is the max number mt_rand() can generate in windows?

Manthan Trivedi
Manthan Trivedi Published in 2018-01-13 04:57:04Z

Actually in my php application i want to assign every user a unique number code. For that i am using mt_rand(0,100000000). I want it to generate millions of number for say millions of different users without repeats and save it to database. Is it possible ? Or any other technique is recommended ? Also to make sure the number doesn't repeat I am checking if the number already exists in the db through php, if it does exist i reload the page to generate another number and save it to db orelse continue to save the number as it is. Is it the best pratice ?

I am new to this non repeating number generator in php so don't know the efficient method.

Aniket Sahrawat
Aniket Sahrawat Reply to 2018-01-13 07:04:16Z

You can use mt_getrandmax() to get the biggest value for mt_rand().

Just as the doc says,

max: Optional highest value to be returned (default: mt_getrandmax())

min max range must be within the range mt_getrandmax(). i.e. (max - min) <= mt_getrandmax() Otherwise, mt_rand() may return poorer random numbers than it should.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323973 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO