Author Topic: Rand() Question  (Read 276 times)

0 Members and 1 Guest are viewing this topic.

Offline stig1Topic starter

  • Enthusiast
  • Posts: 104
    • View Profile
Rand() Question
« on: November 20, 2008, 07:01:51 PM »
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.

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,256
    • View Profile
Re: Rand() Question
« Reply #1 on: November 20, 2008, 07:03:54 PM »
Quote
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.

Offline stig1Topic starter

  • Enthusiast
  • Posts: 104
    • View Profile
Re: Rand() Question
« Reply #2 on: November 20, 2008, 07:13:21 PM »
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.

Offline Mark Baker

  • Addict
  • Posts: 1,586
  • Gender: Male
    • View Profile
Re: Rand() Question
« Reply #3 on: November 21, 2008, 04:02:07 AM »
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

9 out of 10 PHP problems can be resolved by setting
Code: (php) [Select]
error_reporting(E_ALL);
ini_set('display_errors', 1);
php -l <filename> will identify 9 out of the remaining 10 problems
Remember, the command line is your friend
Development Projects: PHPExcel and PHPPowerPoint

Offline Adam

  • Guru
  • Fanatic
  • *
  • Posts: 4,702
  • Gender: Male
    • View Profile
Re: Rand() Question
« Reply #4 on: November 21, 2008, 04:21:26 AM »
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:
Code: [Select]
ALTER TABLE `yourTable` AUTO_INCREMENT = 79000000
Adam
Ronnie Wood, true or false?