C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 18th, 2005, 03:15 PM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old April 18th, 2005, 08:12 PM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
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.

Reply With Quote
  #3  
Old April 18th, 2005, 10:42 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
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.



Reply With Quote
  #4  
Old April 19th, 2005, 07:52 AM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
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...

Reply With Quote
  #5  
Old April 19th, 2005, 05:34 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
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);

Reply With Quote
  #6  
Old April 24th, 2005, 01:52 AM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by Rediahs
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.


Um, that's what I said

Reply With Quote
  #7  
Old April 24th, 2005, 11:59 AM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
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...

Reply With Quote
  #8  
Old April 24th, 2005, 10:35 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
On windows you can use GetTickCount() to return the seconds down to the thousandth of a second.... otherwise look into QueryPerformanceCounter()

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > rand() is weird?


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT