Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > random Number?

random Number?
Thread Tools
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Dec 23, 2002, 10:43 PM
 
Hi,
In java there is lovely old math.random(). Does anyone know if there's anything close in C++?

Thanks!
Gabe
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Dec 24, 2002, 12:07 AM
 
Originally posted by Zimwy:
Hi,
In java there is lovely old math.random(). Does anyone know if there's anything close in C++?
There's srand() and rand().
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Dec 24, 2002, 12:19 AM
 
That's great! Thanks. Where exactly does rand() live? Also, it seems that it gives rather large numbers. I was wondering if you had any advice as to how to get a random number between 0 and 1. Thanks again,

Gabe
(Last edited by Zimwy; Dec 24, 2002 at 12:34 AM. )
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Dec 24, 2002, 01:28 AM
 
rand returns longs, right? So try something along the lines of
Code:
double randomNumber = rand() / sizeof(long);
[vash:~] banana% killall killall
Terminated
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Dec 24, 2002, 01:44 AM
 
Originally posted by Zimwy:
That's great! Thanks. Where exactly does rand() live?
It lives in cstdlib.

Also, it seems that it gives rather large numbers. I was wondering if you had any advice as to how to get a random number between 0 and 1.
The formula I've learned for getting a random number in the range [0, x) is this:
rand() / (RAND_MAX / x)

So to get a number between 0 and 1, with 0 being the minimum and the number always being less than 1, it should be something along these lines:
Code:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main (void) { srand(time(NULL)); cout << (double)rand() / RAND_MAX << endl; return 0; }
RAND_MAX is a constant defined in stdlib.h.

[EDIT:] Real dumb typo in my code. D'oh.
(Last edited by Chuckit; Dec 24, 2002 at 02:17 AM. )
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Dec 24, 2002, 01:53 AM
 
Originally posted by Gul Banana:
rand returns longs, right? So try something along the lines of
Code:
double randomNumber = rand() / sizeof(long);
This won't give you a small number, because sizeof(long) = 4, and 3000000/4 is still a hella big number.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Dec 24, 2002, 02:27 AM
 
Geh. I meant MAX_LONG, of course, not sizeof(long).
[vash:~] banana% killall killall
Terminated
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Dec 24, 2002, 04:06 AM
 
Hey,
Thanks for the great responses. I'm having a little trouble with the seed though. (I'm assuming srand(int) is what controls the seed). If I do:

srand(time(NULL));

then rand() just returns 0 all the time. If I don't ever call srand(), then it will give me random numbers, but always the same order. Does anyone know what to do about this? I've read that it's a good idea to use the time for the random number seed, but it seems that time(NULL) doesn't really cut it.

Thanks a lot!!

Gabe
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Dec 24, 2002, 06:09 AM
 
Originally posted by Zimwy:
Hey,
Thanks for the great responses. I'm having a little trouble with the seed though. (I'm assuming srand(int) is what controls the seed). If I do:

srand(time(NULL));

then rand() just returns 0 all the time. If I don't ever call srand(), then it will give me random numbers, but always the same order. Does anyone know what to do about this? I've read that it's a good idea to use the time for the random number seed, but it seems that time(NULL) doesn't really cut it.
That's very strange. srand(time(NULL)) is a time-honored tradition.
Here's some simple test code I just wrote and compiled that works fine over here; see if it works on your machine.
Code:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main (void) { srand(time(NULL)); for (int i = 0; i < 20; i++) cout << rand(); return 0; }
I just ran it five times, each time getting a random sequence of numbers like you'd expect, so I'm sure the code is fine. So if that doesn't work for you, it sounds like there's something a little funny with your computer.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Dec 24, 2002, 11:01 AM
 
Hi,
Yeah, that gives me a sequence of random numbers too. But, every time I run it, I get the same sequence. That's my problem. Any ideas for why that's happening?
You're only supposed to call srand(time(NULL)) once per program run, right?

Thanks,
Gabe
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Dec 24, 2002, 11:31 AM
 
Never mind. I wasn't actually calling srand(time(NULL)) where I thought I was. Thanks againf or all of your various helps.

Gabe
     
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Dec 24, 2002, 11:36 AM
 
There's also random and srandom ... according to their manpages they're slower but "better".

*shrug*
[Wevah setPostCount:[Wevah postCount] + 1];
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Dec 25, 2002, 06:30 AM
 
Yeah, rand() isn't terribly random.
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 01:58 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2