stig1 Posted November 21, 2008 Share Posted November 21, 2008 I am interested in using rand() to generate customer order numbers between a certain range. For example between 79000000 - 79999999 What are the chances of the rand() generating the same number twice, at the same time, heaps of people place an order into our system. I am planning to check the orders table with the generated rand() before inserting the order number into the system. I have never used rand() before, so i'm just checking, cause otherwise I would just do 79000001, 79000002, etc for order numbers. However I didn't want customers to be thinkin they were only order 1, or 2, etc. Quote Link to comment https://forums.phpfreaks.com/topic/133574-rand-question/ Share on other sites More sharing options...
trq Posted November 21, 2008 Share Posted November 21, 2008 I am planning to check the orders table with the generated rand() before inserting the order number into the system. Then whats it matter? If the generated number exists, just generate another. Quote Link to comment https://forums.phpfreaks.com/topic/133574-rand-question/#findComment-694797 Share on other sites More sharing options...
stig1 Posted November 21, 2008 Author Share Posted November 21, 2008 What happens if the script is executed say 10-20 times at once, and say 4 of those have the same random number generated but haven't been inserted into the database yet, but once 1 of them is generated that number is then taken, therefore the other 3 scripts all have the same number and will all produce errors when trying to insert into the database as the number already exists... how do we by pass this problem? Only can have one order number per each order. Quote Link to comment https://forums.phpfreaks.com/topic/133574-rand-question/#findComment-694815 Share on other sites More sharing options...
Mark Baker Posted November 21, 2008 Share Posted November 21, 2008 What are the chances of the rand() generating the same number twice, at the same time, heaps of people place an order into our system. 79999999 - 79000000 = 999999 Assuming equal distribution, then the probability of the same number being generated twice is 1 in 999999. The probability of generating the same number twice, at the same time depends on how many people are placing orders at the same time. Rather than using random numbers, why not start with a base number 79132684, and then increment by a value like 17 every time somebody places an order. Then, there's no obvious pattern that any one customer is likely to see Quote Link to comment https://forums.phpfreaks.com/topic/133574-rand-question/#findComment-695203 Share on other sites More sharing options...
Adam Posted November 21, 2008 Share Posted November 21, 2008 Or could just use auto_increment in the database and set the starting number to say 79000000 .. ? To do that in PHPMyAdmin just click on the "operations" tab on the table structure view page, should be a field for "auto_increment" .. Or as an SQL query: ALTER TABLE `yourTable` AUTO_INCREMENT = 79000000 Adam Quote Link to comment https://forums.phpfreaks.com/topic/133574-rand-question/#findComment-695215 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.