 |
 |
random Number?
|
 |
|
 |
|
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status:
Offline
|
|
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
|
|
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'."
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
Geh. I meant MAX_LONG, of course, not sizeof(long).
|
|
[vash:~] banana% killall killall
Terminated
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status:
Offline
|
|
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
|
|
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'."
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status:
Offline
|
|
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
|
|
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
|
|
Yeah, rand() isn't terribly random.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|