creating random numbers

Thread Solved

Join Date: Apr 2006
Posts: 15
Reputation: puppy is an unknown quantity at this point 
Solved Threads: 0
puppy puppy is offline Offline
Newbie Poster

creating random numbers

 
0
  #1
Apr 29th, 2006
Hi.

If I want to generate random numbers between -1 and 1, do I need to write a function to do that or is their something in the stdlib that can do it?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: creating random numbers

 
0
  #2
Apr 30th, 2006
See srand and rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,397
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: creating random numbers

 
0
  #3
Apr 30th, 2006
>>If I want to generate random numbers between -1 and 1

Why? The only number between -1 and 1 is 0. -1, 0 and 1 are all integers, there are no fractions in integer math. rand() returns an integer. So if you want a float return value, such as 0.123 then you will have to write a function that puts the return value from rand() into a float variable then divide it by RAND_MAX (defined in stdlib.h) to make it less than 1. I'm not sure how to make a random number between -1.0 and 0 other than just simply arbitrarily make the result from the above calculations negative.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 238
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: creating random numbers

 
0
  #4
Apr 30th, 2006
Originally Posted by puppy
If I want to generate random numbers between -1 and 1, do I need to write a function to do that or is their something in the stdlib that can do it?
Something like this, perhaps?
  1. double drand(void)
  2. {
  3. return 2.0 * rand() / (RAND_MAX + 1.0) - 1.0;
  4. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 15
Reputation: puppy is an unknown quantity at this point 
Solved Threads: 0
puppy puppy is offline Offline
Newbie Poster

Re: creating random numbers

 
0
  #5
May 1st, 2006
Thanks everyone. I meant a floating decimal between -1 and 1, not an int. I've got it now. Thanks again.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC