The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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-18-2008 12:13 AM
how do i generate random integer using only shell script sridhusha Shell Programming and Scripting 1 12-31-2007 03:46 AM
Generate a random password chiru_h Shell Programming and Scripting 5 10-07-2007 08:03 PM
Random number generation in ksh mervin2006 UNIX for Dummies Questions & Answers 2 04-27-2007 02:02 AM
generate random number in korn shell frustrated1 Shell Programming and Scripting 2 12-31-2005 07:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-15-2001
MacMonster MacMonster is offline
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?
  #2 (permalink)  
Old 10-15-2001
jyotipg jyotipg is offline
Registered User
  
 

Join Date: Oct 2001
Location: Bangalore
Posts: 95
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..
  #3 (permalink)  
Old 10-15-2001
PxT's Avatar
PxT PxT is offline Forum Advisor  
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0