| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
rand() is weird?
I am trying to use random numbers for a program, but they are not working as expected.
I am initializing the random seed like this: Code:
srand(time(NULL)); I am using rand() in the program like this: Code:
cout << maxNum * rand()/RAND_MAX << "\n"; The odd thing about it is, when I used it in my program, it was always turning up the same number - it occasionally changed, but this was not often enough.. So I investigated and made a small program that does this: Code:
srand(time(NULL));
for (int i=0; i<5;i++ ){
cout << maxNum * rand()/RAND_MAX << "\n";
}
What happens, is that in the loop, the numbers after the first one seem to be increasingly more and more random. Is this normal? Here are some sample outputs. 7 2 4 6 2 7 9 5 1 8 7 2 0 9 2 The first number rarely changes. I figure it has something to do with how the time is brought in, but what? To loop and use the last obtained number doesn't seem to make a lot of sense to me, though if nobody has any other solutions I suppose I could use it. |
|
#2
|
|||
|
|||
|
Ahhh. I found that even the non-random rand() % maxNum works fine for my purposes, though I still don't understand why what I stated earlier is giving me such strange results.
|
|
#3
|
||||
|
||||
|
This is because you have to use srand() EVERY time before you use rand.... oftentimes I just write a function like my_rand() that automatically seeds using srand(time(NULL)) and gerates the random number and mods it by the arguement that I pass....
also, look at srand(time(NULL)) for a second, srand() will only seed rand() differently if it is provided with different input, but in a tight loop (like yours) the loop will execute in just a split second, and time(NULL) will never increase since it returns a number of whole seconds.... if you need to do a tight count like that, I recommend using a different seed for srand(), like maybe GetTickCount() (if you're on Windows)....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#4
|
|||
|
|||
|
Pardon, but I'm not sure you understood...
I am not using rand() in a loop... I am using it once when my program starts up. But every time I run my program, the number is the same - it does not act random. i.e. for about 10 minutes or so, it is 9, then it's 1 for just as long... I decided to experiment, so I wrote a program that would put it in a loop, and lo and behold, the numbers following the first number were more frequently randomated. The first number never changed. As you can see in my examples. I also heard that setting srand() more than once is a bad idea... it's not like I could, though, because my program does rand() only ONCE.. hope you understand what I'm saying. The program that runs it 5 times was only a test, you could say it's a debug, I was trying to figure out what it's actually doing. My program is just a guessing game - it picks a random number and you guess. It gives an indication of whether it is higher or lower - here's the code, hope you understand now. Code:
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main(){
srand(time(NULL));
const int maxNum = 10;
const int target = maxNum * (rand()/RAND_MAX);
int guess;
int count = 0;
cout << "Guess a number between 1 and " << maxNum << "!\n";
do{
cin.clear();
cin >> guess;
if (!cin){
cout << "I said a number!\n";
}else{
count++;
if (guess>target){
cout << "Lower!\n";
} else if (guess<target){
cout << "Higher!\n";
}
}
}
while (guess!=target);
if (guess=target){
cout << "You guessed correctly! You took " << count;
if (count==1){
cout << " turn.\n";
}else{
cout << " turns.\n";
}
}
return 0;
}
Please ignore the other errors in the program, I'm sure they've got nothing to do with why it is doing this... ![]() |
|
#5
|
||||
|
||||
|
hm, that code works fine for me....
try setting your target this way: Code:
const int maxNum = 10; const target = maxNum * (rand()/RAND_MAX); int guess,int count = 0; srand(time(NULL)); target = ((rand() + 1) % maxNum); |
|
#6
|
|||
|
|||
|
Quote:
Um, that's what I said ![]() |
|
#7
|
|||
|
|||
|
Okay, this is what I'm trying to say...
These are examples of the unwanted results that are occurring. (each line represents a new run of the program, and each space is a new iteration of the loop) rand() % num : 0 6 8 0 8 3 7 2 5 2 3 7 2 5 2 6 5 8 0 9 6 5 8 0 9 num * rand()/RAND_MAX: 7 2 4 6 2 7 9 5 1 8 7 2 0 9 2 7 5 2 8 1 DESIRED result is something like this: 7 2 4 6 2 5 9 5 1 8 3 2 0 9 2 2 5 2 8 1 random even within 1 second of running the program last... Is this clearer? I heard that num * rand()/RAND_MAX was the best way to do it because rand() % num isn't actually random, or something... but rand() % num is closer to what I want.... num * rand()/RAND_MAX is really odd because it keeps giving me the same initial value when I run the program within around 10 minutes of running it last. Maybe is there a way to get time() to give milliseconds too so that the seed is updated faster than 1 second... |
|
#8
|
||||
|
||||
|
On windows you can use GetTickCount() to return the seconds down to the thousandth of a second.... otherwise look into QueryPerformanceCounter()
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > rand() is weird? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|