
japhy
Enthusiast
Aug 29, 2000, 5:00 PM
Post #2 of 6
(768 views)
|
|
Re: Best way to generate random number
[In reply to]
|
Can't Post
|
|
Firstly, rand() is a pretty good way of generating a random number. Unless you're in need of serious security, rand() is fine. rand(EXPR) returns a random number (NOT NECESSARILY AN INTEGER) from 0 (inclusive) to EXPR (exclusive). So rand(10) will NEVER return 10, but could return any number less than 10, and greater than or equal to 0. If you want a random number, between 1 and 7 digits, it would range from 1 to 9999999. However, int(rand(9999999)) would return an integer greater than or equal to 0, and less than or equal to 9999998. So, add 1 to that, and then pad the number to seven digits: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> sprintf "%07d", 1 + rand 9999999; </pre><HR></BLOCKQUOTE> ------------------ Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher
|