Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/133574-rand-question/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/133574-rand-question/#findComment-694815
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/133574-rand-question/#findComment-695203
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/133574-rand-question/#findComment-695215
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.