The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
How do I create a random number in PHP?
Discuss How do I create a random number in PHP? in the PHP Development forum on Dev Shed. How do I create a random number in PHP? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 5th, 2000, 02:52 PM
|
 |
Full Access
|
|
Join Date: Jun 2000
Location: London, UK
Posts: 2,019
Time spent in forums: 3 sec
Reputation Power: 15
|
|
|
I am working on a project that requires each article to be assigned a unique nine-digit ID number that will be stored in a mySQL database along with some other info. I know I can use the PRIMARY KEY function to make sure it's unique in mySQL but my problem lies in the fact that I don't know how to generate a random 9-digit number in PHP! Can someone please help me out here? Thanks.
--------------------
Alex
(http://www.alex-greg.co.uk)
|

June 5th, 2000, 02:56 PM
|
|
Contributing User
|
|
Join Date: Apr 1999
Posts: 114
Time spent in forums: 42 m 44 sec
Reputation Power: 15
|
|
|

June 5th, 2000, 03:28 PM
|
 |
Full Access
|
|
Join Date: Jun 2000
Location: London, UK
Posts: 2,019
Time spent in forums: 3 sec
Reputation Power: 15
|
|
|
The point you're missing is that it always has to be nine digits long: with no zeros at the start. How do I do this?
|

June 5th, 2000, 03:38 PM
|
|
Contributing User
|
|
Join Date: Apr 1999
Posts: 114
Time spent in forums: 42 m 44 sec
Reputation Power: 15
|
|
|
Doesn't rand (100000000, 999999999) work?
|

June 5th, 2000, 03:55 PM
|
 |
Full Access
|
|
Join Date: Jun 2000
Location: London, UK
Posts: 2,019
Time spent in forums: 3 sec
Reputation Power: 15
|
|
|
OK that now works, thanks, but the number stays the same even when I refresh the page: how can I get it so that the number's different each time?
|

June 6th, 2000, 09:08 AM
|
|
Junior Member
|
|
Join Date: May 2000
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
You should be able to find that information at the link scollo posted..
|

June 6th, 2000, 11:16 AM
|
|
Gödelian monster
|
|
Join Date: Jul 1999
Location: Central Florida, USA
|
|
|
Why not just generate a number based on the current date, time, & second? It also might provide a useful way of tracking items later.
|

June 7th, 2000, 11:59 AM
|
 |
Full Access
|
|
Join Date: Jun 2000
Location: London, UK
Posts: 2,019
Time spent in forums: 3 sec
Reputation Power: 15
|
|
|
OK, how does that work? Don't forget you're dealing with a novice here!
|

June 7th, 2000, 12:20 PM
|
|
Contributing User
|
|
Join Date: Apr 1999
Posts: 114
Time spent in forums: 42 m 44 sec
Reputation Power: 15
|
|
|
Hi Alex,
Sorry. Didn't mean to frustrate you. You need to call the srand() function before you call the rand() function. This causes the number to be different every time.
Try this:
srand (time());
$iNum = rand (100000000, 999999999);
-- Christopher
|

June 7th, 2000, 03:14 PM
|
 |
Full Access
|
|
Join Date: Jun 2000
Location: London, UK
Posts: 2,019
Time spent in forums: 3 sec
Reputation Power: 15
|
|
|
Sweet, it works. Thanks guys!
[Topic closed]
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|