I need a function that generates a random number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

I need a function that generates a random number

Post 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, :drunk:

Batoe
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: I need a function that generates a random number

Post 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();
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: I need a function that generates a random number

Post by onion2k »

To answer the question..

Code: Select all

$rand = rand(100000, 999999);
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".
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: I need a function that generates a random number

Post by aditya2071990 »

Oh guys, thank you so much! I have been looking for the same thing! And the solution has already been posted!
Post Reply