Thread: Returns the same value

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    67

    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?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You only need to srand once in your code..call it in main

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    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!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I ran that code and it worked for me. Very odd. What compiler/OS are you running?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> 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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    67
    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

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What do you mean, srand() has to be called a lot? You've just been told you only need to call it once.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    67

    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 !

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    67
    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..
    Last edited by TeQno; 07-09-2003 at 05:43 AM.

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    67
    wow :P

    allright i wont be such a pain in the arse in the future :P
    Last edited by TeQno; 07-09-2003 at 05:58 AM.

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    67
    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++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 03:51 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM