The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
generate random number in perl zx1106 Shell Programming and Scripting 2 03-17-2008 08:13 PM
how do i generate random integer using only shell script sridhusha Shell Programming and Scripting 1 12-30-2007 11:46 PM
Generate a random password chiru_h Shell Programming and Scripting 5 10-07-2007 05:03 PM
Random number generation in ksh mervin2006 UNIX for Dummies Questions & Answers 2 04-26-2007 11:02 PM
generate random number in korn shell frustrated1 Shell Programming and Scripting 2 12-31-2005 03:49 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-14-2001
Registered User
 

Join Date: Oct 2001
Location: Null
Posts: 16
Question How to generate a random number?

How to generate a random integer with specific range(for example, from 1 to 1000)?

Also, how to convert a floating point number into a integer?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-14-2001
Registered User
 

Join Date: Oct 2001
Location: Bangalore
Posts: 93
You can use srand() to generate a random number. Then use the modulus operator to get a number between 1 and 1000. I dont remember the exact syntax..but it would something like

your_random_number = srand() % 1001

Good Luck..
Reply With Quote
  #3 (permalink)  
Old 10-15-2001
PxT's Avatar
PxT PxT is offline
Registered User
 

Join Date: Oct 2000
Location: Sacramento, CA
Posts: 909
From the linux rand(3) man page:

Quote:
The versions of rand() and srand() in the Linux C Library use the same random number gen*
erator as random() and srandom(), so the lower-order bits should be as random as the
higher-order bits. However, on older rand() implementations, the lower-order bits are
much less random than the higher-order bits.

In Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P.
Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press,
1992 (2nd ed., p. 277)), the following comments are made:
"If you want to generate a random integer between 1 and 10, you should always do
it by using high-order bits, as in

j=1+(int)(10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+(rand() % 10);

(which uses lower-order bits)."
In other words, under Linux your algorithm is probably fine, on other architectures it may not be very random. For portability, use the noted syntax or check the man page for your particular rand implementation.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0