CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Best way to generate random number

 



Warren Bell
Deleted

Aug 29, 2000, 4:44 PM

Post #1 of 6 (1132 views)
Best way to generate random number Can't Post

What the best/esiest way to generate a random number? I need a number 7 digits long from 1 to 9999999.

Right now I'm using:

sprintf "%07d", rand 9_000_000;

Is this good enough? I don't know anything about rand or the number at the end.


japhy
Enthusiast

Aug 29, 2000, 5:00 PM

Post #2 of 6 (1132 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



Warren Bell
Deleted

Aug 29, 2000, 5:34 PM

Post #3 of 6 (1132 views)
Re: Best way to generate random number [In reply to] Can't Post

Thanks. A couple questions, I found the code I posted in a search. Why was it in the format 1_000_000? Is it the same to use numbers without underscores like your example?

Also, what kind of security risk does rand() have? I don't think what I'm using it for matter but just curious.


japhy
Enthusiast

Aug 29, 2000, 5:51 PM

Post #4 of 6 (1132 views)
Re: Best way to generate random number [In reply to] Can't Post

You can place underscores in a number where you'd expect to find commas (thousands, millions, billions, etc). So 13_438_832 is easier to read than 13438832.

As for the "security risk", I'm just saying that rand() uses a seed to determine its random numbers. It's not GENUINELY random. There's some algorithm behind it.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



dws
Deleted

Aug 30, 2000, 2:29 PM

Post #5 of 6 (1132 views)
Re: Best way to generate random number [In reply to] Can't Post

Don't forget to call srand() to seed the random number generator. An easy way to do it is to seed it with the time, though that only give you 86,400 unique starting numbers per day. A slightly more sophisticated method involves factoring in the Unix process id, via

srand(time() ^ $$)

Neither method is good enough if your needs are truly hardcore, but should suffice for lightweight randomness.


japhy
Enthusiast

Aug 30, 2000, 3:19 PM

Post #6 of 6 (1132 views)
Re: Best way to generate random number [In reply to] Can't Post

Calling srand() is unnecessary in recent Perls.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives