-
Returns the same value
hi i wrote a piece of code
Code:
#include "console.h"
#include <iostream>
#include <stdlib.h>
#include "lib.h"
#include <time.h>
#include <windows.h>
//#define MAX_WALK 25
using namespace std;
int StartX = RandX();
int StartY = RandY();
int Direc;
int RandDir()
{
srand(time(NULL));
Direc = rand()%4;
if (Direc == 0)
{
cout << "Direction 0";
}
if (Direc == 1)
{
cout << "Direction 1";
}
if (Direc == 2)
{
cout << "Direction 2";
}
if (Direc == 3)
{
cout << "Direction 3";
}
return Direc;
}
int main()
{
/* int KeyPress;
KeyPress = PeekVirtualKey();
int TimesToWalk;
MoveCursor(StartX, StartY);
cout << "*";
IGNORE OUT-COMMENTED ITEMS PLEASE
*/
RandDir();
RandDir();
RandDir();
RandDir();
RandDir();
RandDir();
return 0;
}
upon exection it returns the same value for RandDir(); over and over, what did I miss?
-
You only need to srand once in your code..call it in main
-
Move the call to srand() rand out of the function and into your main program so you only call it once. You are basically setting the random seed to the same value and restarting the pseudo random sequence on each call.
*** EDIT ***
Ooop's parallelism!
-
I ran that code and it worked for me. Very odd. What compiler/OS are you running?
-
adrianxw, do the results that teqno had happen because the time(NULL) return value doesn't change between iterations? I ran slightly different code, where the time(NULL) value did change, and my results were fairly random.
-
>>> do the results that teqno had happen because the time(NULL) return value doesn't change between iterations?
Yes, on a fast machine, (and most of todays machines are fast enough), calling time() several times in quick succesion will return the same value. Thus, the random number seed will be the same, and the generated pseudo random sequence will also be the same. If he changed it so there was a call to Sleep() with a suitable delay between the function calls, then it would work as it is, but the best advice is always to seed the random number generator only once.
-
no, the srand function needs to be called a lot.. until I end it manually
adrianxw: I think thats the problem.. I'll implement sleep and check the results.. thanx
-
What do you mean, srand() has to be called a lot? You've just been told you only need to call it once.
-
Sleep(1000)
bennyandthejets: I need to call it alot okay?
I changed the RandDir fucntion to:
Code:
int RandDir()
{
Sleep(1000);
srand(time(NULL));
Direc = rand()%4;
if (Direc == 0)
{
cout << "Direction 0";
}
if (Direc == 1)
{
cout << "Direction 1";
}
if (Direc == 2)
{
cout << "Direction 2";
}
if (Direc == 3)
{
cout << "Direction 3";
}
return Direc;
}
and it works like a charm now :) thanx !
-
I need to call that function alot, because I need to generate alot of random numbers and I understand it wont be anymore random thats obvious..
see the code
Code:
#include "console.h"
#include <iostream>
#include <stdlib.h>
#include "lib.h"
#include <time.h>
#include <windows.h>
#define MAX_WALK 25
using namespace std;
int StartX = RandX();
int StartY = RandY();
int Direc;
int MyEndKey;
int RandDir()
{
Sleep(1000);
srand(time(NULL));
Direc = rand()%4;
if (Direc == 0)
{
MoveCursor(StartX, StartY);
cout << " ";
StartY--;
MoveCursor(StartX, StartY);
cout << "*";
}
if (Direc == 1)
{
MoveCursor(StartX, StartY);
cout << " ";
StartY++;
MoveCursor(StartX, StartY);
cout << "*";
}
if (Direc == 2)
{
MoveCursor(StartX, StartY);
cout << " ";
StartX--;
MoveCursor(StartX, StartY);
cout << "*";
}
if (Direc == 3)
{
MoveCursor(StartX, StartY);
cout << " ";
StartX++;
MoveCursor(StartX, StartY);
cout << "*";
}
return Direc;
}
int main()
{
MoveCursor(StartX, StartY);
cout << "*";
do
{
int MyEndKey;
MyEndKey = PeekVirtualKey();
RandDir();
} while (MyEndKey != VK_ESCAPE);
return 0;
}
i just wanted to test it, thats why I did what I did, and i got te same values if i repeated RandDir really fast, I needed to know why thats all. I got the same value over and over and all I needed to know was how to change that, i know now, thanx for that..
-
wow :P
allright i wont be such a pain in the arse in the future :P
-
There's a point to which you can persist with your argument, that is, only while you provide some actual solid evidence to back it up. You never actually said why you needed to call a lot of srand()s, given that you were told how it works.
-
well, I thought it was an essential part to generate the number and I didn't really figure out what you exactly meant, after seeing Salems example i figured it out, but at the moment its all changed again :-)
its only my second day to c++ ;)