random functions

This is a discussion on random functions within the C++ Programming forums, part of the General Programming Boards category; The following code has two random functions that don't pick random. THEY ALLWAYS PICK 0!!!!can anyone help me??? <near the ...

  1. #1
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269

    random functions

    The following code has two random functions that don't pick random. THEY ALLWAYS PICK 0!!!!can anyone help me???

    <near the bottem>



    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    int main()
    {
    clrscr();
    int tries;
    int x;
    int y;
    int a = 0;
    int b = 0;
    int plyrs;
    cout<<"how many players?<2 for 2 and anything for 1>"<<endl;
    cin>>plyrs;
    if (plyrs==2)
    {
    cout<<"please enter amount o' tries: "<<endl;
    cin>>tries;
    clrscr();
    cout<<"hider, type coordinates of hidden character:<has to be under 10,10 please."<<endl;
    cin>>x>>y;
    clrscr();
    cout<<"seeker, you are at coordinates 0,0. you will have to find the hidden character in 15 tries. move up down left and right. good luck."<<endl;
    getch();
    clrscr;
    {
    int c = 0;
    while(c==0)
    {
    if (tries>0)
    {
    if (b==x&&a==y)
    {
    cout<<"congratulations!you've found the hidden item!!!!"<<endl;
    getch();
    return 0;
    }
    else
    {
    clrscr();
    cout<<"coordinates="<<b<<", "<<a<<endl;
    cout<<"you have "<<tries<<"tries left."<<endl;
    gotoxy(b, a);
    char cl = getch();
    if (cl==72)
    {
    gotoxy(b, a);
    putch('$');
    gotoxy(b, a++);
    tries=tries-1;
    }
    if (cl==104)
    {
    if (tries<=10)
    {
    cout<<"first coordinate is: "<<x<<endl;
    getch();
    }
    }
    if (cl==80)
    {
    gotoxy(b, a);
    putch('$');
    gotoxy(b, a--);
    tries=tries-1;
    }
    if (cl==63)
    {
    cout<<"its not nice to cheat but, oh well. the coordinates are: "<<x<<", "<<y<<endl;
    getch();
    }
    if (cl==77)
    {
    gotoxy(b, a);
    putch('$');
    gotoxy(b--, a);
    tries=tries-1;
    }
    if (cl==75)
    {
    gotoxy(b, a);
    putch('$');
    gotoxy(b++, a);
    tries=tries-1;
    }
    if (cl==27)
    {
    getch();
    return 0;
    }
    }
    }
    else
    {
    cout<<"im sorry but you ran out of tries!!!"<<endl;
    getch();
    return 0;
    }
    }
    }
    return 0;
    }
    else
    {
    cout<<"ok, computer. the computer will now detirmine the coordinates...."<<endl;
    int x = rand()%16;//RANDOM OUT OF 15!!!!
    int y = rand()%16;//RANDOM OUT OF 15!!!
    cout<<"the coordinates are out of 15."<<endl;
    clrscr();
    cout<<"how many tries?"<<endl;
    cin>>tries;
    clrscr();
    {
    cout<<"you have "<<tries<<" tries to guess the coordinates."<<endl;
    cout<<"your coordinates are: "<<a<<", "<<b<<endl;
    if (tries>0)
    {
    int d =5;
    while(d==5)
    {
    if (b!=x, a!=y)
    {
    char cl = getch();
    if (cl==72)
    {
    gotoxy(b, a);
    gotoxy(b, a++);
    tries=tries-1;
    }
    if (cl==80)
    {
    gotoxy(b, a);
    gotoxy(b, a--);
    tries=tries-1;
    }
    if (cl==75)
    {
    gotoxy(b, a);
    gotoxy(b--, a);
    tries=tries-1;
    }
    if (cl==77)
    {
    gotoxy(b, a);
    gotoxy(b++, a);
    tries=tries-1;
    }
    if (cl==104)
    {
    if (tries<=10)
    {
    cout<<"the hint is: "<<x<<" is the first coordinet"<<endl;
    }
    }
    else
    {
    cout<<"congratulations!!! you found the right coordinates!!"<<endl;
    }
    }
    }
    }
    else
    {
    cout<<"sorry, but tries ran out!!"<<endl;
    }
    }
    }
    return 0;
    }


    Please help me if you can!!!
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You have to seed the rand() function using srand() if you want it to produce a different series of random numbers everytime your code is run, check the FAQ.
    zen

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    The rand function produces a random number at compile time, therefore to get a random number each time you execute your program you would need to re-compile it, not very good. However you can use the function srand to seed the rand function, for example you can base the rand function on the time, therefore producing a different number each time.
    Code:
    int stime;
    long ltime;
    
    ltime = time(NULL);
    stime = (unsigned) ltime/2;
    srand(stime);
    
    printf("%d", rand()%16);
    This time you should get a different number each time.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The rand function produces a random number at compile time, therefore to get a random number each time you execute your program you would need to re-compile it
    I don't think it makes any difference when it's compiled, rand() will take a default seed value and this won't be influenced by when it's compiled.
    zen

  5. #5
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    have you guys tried my game?? i like it.
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok I said you could have my code if you posted yours so here goes.... sorry about the length but I dont have a zip util on me at the mo so I will cut and paste.

    Code:
    Board.h
    
    #ifndef BOARD_H
    #define BOARD_H
    
    
    class Board
    		{
    			private:
    						char TheBoard [10][10];
    						int HurkleX;
    						int HurkleY;
    
    						void Miss(int,int);
    						void Hit();
    						void Display(int,int)const;
    						void Win(int,int)const;
    						char* Direction(int,int)const;
    			public:
    						Board();
    						~Board() {}
    						void Play();
    						
    		};
    
    #endif
    Code:
    Board.cpp
    
    #include "Board.h"
    #include "ScreenFuncs.h"
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <cctype>
    
    using namespace std;
    
    
    Board::Board()
    		{
    			srand ((unsigned)time(NULL));
    			HurkleX=rand()%10;
    			HurkleY=rand()%10;
    			for (int i=0;i<10;i++)
    				for(int j=0;j<10;j++)
    					TheBoard [i][j]='.';
    		}
    
    void Board::Miss(int x,int y)
    		{
    			TheBoard [y][x]='X';
    			gotoxy(2,20);
    			cout<<"Missed the Hurkle but he's "<<Direction(x,y)<<" from there.          "<<flush;
    		}
    
    void Board::Hit()
    		{
    			TheBoard [HurkleY][HurkleX]='H';
    		}
    
    void Board::Win(int x,int y)const
    		{
    			gotoxy(x,y);
    			cout<<"You have won!!!! You found the Hurkle!!!!          "<<endl;
    		}
    
    void Board::Display(int x,int y)const
    		{
    			gotoxy(x,y);
    			cout<<"  0  1  2  3  4  5  6  7  8  9"<<flush;
    			y++;
    			gotoxy(x,y);
    			for(int i=0;i<10;i++)
    			{
    				cout<<i<<" "<<flush;
    				for(int j=0;j<10;j++)
    				{
    					cout<<TheBoard[i][j]<<"  ";
    					if (j==9)
    					{
    						cout<<flush;
    						y++;
    					}
    				}
    				gotoxy(x,y);
    			}
    		}
    
    void Board::Play()
    {
    	int Tries=10,x=0,y=0;
    	
    	do
    	{
    		Display(2,2);
    		gotoxy(2,14);
    		cout<<"Where do you think the Hurkle lies...."<<endl<<"  Enter X Position (0-9) ?      \b\b\b\b\b";
    		cin>>x;
    		cout<<"  Enter Y Position (0-9) ?      \b\b\b\b\b";
    		cin>>y;
    		if (x<0 || x>9)continue;
    		if (y<0 || y>9)continue;
    		if (x==HurkleX && y==HurkleY)
    		{
    			Hit();
    			Display(2,2);
    			Win(2,20);
    			break;
    		}
    		else
    		{
    			Miss(x,y);
    		}
    
    		Tries--;
    		gotoxy(2,18);
    		cout<<"Tries left = "<<Tries;
    	}
    	while(Tries);
    	if (x==HurkleX && y==HurkleY) return;
    	gotoxy(2,20);
    	cout<<"Sorry you have lost.Better luck next time.        "<<endl<<"  The Hurkle was at X-Pos :- "<<HurkleX<<" and Y-Pos :- "<<HurkleY<<endl;
    }			
    
    char* Board::Direction(int x,int y)const
    {
    	int DistX=(HurkleX-x);
    	int DistY=(HurkleY-y);
    	if(abs(DistX)>abs(DistY))
    		if (DistX < 0)
    			return "LEFT";
    		else return "RIGHT";
    	if(abs(DistX)<abs(DistY))
    		if (DistY < 0)
    			return "UP";
    		else return "DOWN";
    	if (abs(DistX)==abs(DistY))
    	{
    		int a=rand()%2;
    		if (a)
    		{
    			if (DistX < 0)
    			return "LEFT";
    		else return "RIGHT";
    		}
    		else
    		{
    			if (DistY < 0)
    			return "UP";
    		else return "DOWN";
    		}
    	}
    }
    Code:
    ScreenFuncs.h
    
    void clrscr();
    void gotoxy(int,int);
    Code:
    ScreenFuncs.cpp
    
    #include <windows.h>
    #include "ScreenFuncs.h"
    
    // clearscreen function
    
    void clrscr()
    {
       COORD coordScreen = { 0, 0 };
       DWORD cCharsWritten;
       CONSOLE_SCREEN_BUFFER_INFO csbi;
       DWORD dwConSize;
       HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
       GetConsoleScreenBufferInfo(hConsole, &csbi);
       dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
       FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
       GetConsoleScreenBufferInfo(hConsole, &csbi);
       FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
       SetConsoleCursorPosition(hConsole, coordScreen);
    }
    
    // gotoxy function
    
    void gotoxy(int x, int y) 
    { 
        COORD point; 
        point.X = x; point.Y = y; 
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point); 
    }
    Code:
    Main.cpp
    
    #include "Board.h"
    
    int main()
    {
    	Board Game;
    	Game.Play();
    	
    	return 0;
    }
    Enjoy.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  2. Replies: 3
    Last Post: 09-30-2008, 12:10 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Random function's application:Dice Roller
    By angeljicu in forum C Programming
    Replies: 0
    Last Post: 11-02-2002, 04:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21