Page 1 of 1
I need a function that generates a random number
Posted: Sun Sep 07, 2008 10:25 pm
by cap2cap10

Hey, php technorati. I need a function that generates a random number and then creates a variable that will be posted to a mysql table. I now how to generate a random #, but not how to set it equal to a variable. Here is the random number code:
$rand(10000000, 99999999)
I am trying to create orderID number for every new purchase, which is why I need to number added to the database via declaration of a variable $orderID. Can anyone create this funtion. Thanks in advance,
Batoe
Re: I need a function that generates a random number
Posted: Sun Sep 07, 2008 11:13 pm
by starram
Instead of using rand function how about using time() ?
Your rand function can generate duplicate number but time() will not, Or with rand use time() function.
$a=rand(0,400)+time();
Re: I need a function that generates a random number
Posted: Mon Sep 08, 2008 2:55 am
by onion2k
To answer the question..
Starram is right though, you will need to guard against duplicate order IDs. Personally, on the store applications I create, I use the auto incremented INT value of the database record, along with a code for the order type, a second code for the app version, and then pad that out with random numbers so IDs will be the same length into the future.. so the order id ends up being something like "ORDERTYPE-VERSION-ORDERID-RAND".
Re: I need a function that generates a random number
Posted: Sat Oct 11, 2008 4:29 am
by aditya2071990
Oh guys, thank you so much! I have been looking for the same thing! And the solution has already been posted!