Output 3 random sets of 5

This is a discussion on Output 3 random sets of 5 within the C++ Programming forums, part of the General Programming Boards category; I'm writting a Yahtzee program and so far it's going okay. Write now I'm trying to display 3 sets of ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    Output 3 random sets of 5

    I'm writting a Yahtzee program and so far it's going okay. Write now I'm trying to display 3 sets of random numbers. Each set is to have 5 numbers. Well, I have 3 sets displaying, but each set looks the same. Example:

    13146
    13146
    13146

    Should be different, like:

    13146
    53614
    64135

    Cane someone tell me where I'm faltering in my code? Here's what I have:

    Code:
    #include "Player.h"
     
    int main()
    
    {
    	Player aPlayer("John Sparks");
    	aPlayer.displayPlayer();
    	int i;
    	for(i=0; i<3; i++)
    	{
    	aPlayer.rollThem();
    	}
    		return 0;
    }
    
    #include <stdio.h>
    #include <iostream.h>
    #include<conio.h>
    #include<string.h>
    #include "Die.h"
    
    const int MAX_NAME = 15;
    
    class Player
    {
    private:
    	char playerName[MAX_NAME];
    	Die rollDie;
    	Die MAX_DIE[5];
    public:
    	Player(char *name);
    	void setPlayer(char *name);
    	void displayPlayer();
    	char *getName();
    	void rollThem();
    };
    
    void Player::rollThem()
    {
    	Die myDie;
    	int i;
    	for(i = 0; i < 5; i++)
    	{
    		myDie.rollDie();
    		myDie.displayValue();
    	}
    	cout<<endl;
    }
    
    char *Player::getName()
    {
    	return playerName;
    }
    
    Player::Player(char *name)
    {
    	strcpy(playerName, name);
    }
    
    void Player::displayPlayer()
    {
    	cout<<playerName<<' '<<endl;
    }
    
    void Player::setPlayer(char *name)
    {
    	cout<<"Enter name ";
    	cin>>playerName;
    	cout<<cout<<"Player name is "<<playerName<<'.'<<' '<<endl;
    }
    
    #include <stdlib.h>  
    #include <stdio.h>
    #include <time.h>
    #include <iostream.h>
    
    const int MAX_DIE = 5;
    
    class Die
    {
    private:
    	int   dieFaceVal;
    	bool dieRoll;
    
    public:
    	Die();
    	int    getFaceVal();
    	void setFaceVal(int newVal);
    	void rollDie();
    	void dieTrue();
    	void dieFalse();
    	void displayValue();
    };
    
    Die::Die()
    {
    	srand((unsigned)time(NULL));
    	dieRoll = true;
    }
    int Die::getFaceVal()
    {
    	return dieFaceVal;
    }
    
    void Die::rollDie()
    {
    if (dieRoll == true)
    	{
    		dieFaceVal = ((rand() % 6) + 1);
    	}
    }
    
    void Die::dieTrue()
    {
    	dieRoll = true;
    }
    
    void Die::dieFalse()
    {
    	dieRoll = false;
    }
    
    void Die::setFaceVal(int newVal)
    {
    	dieFaceVal = newVal;
    }
    
    void Die::displayValue()
    {
    	cout<<dieFaceVal<<' ';
    }

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    yeah... add the line

    srand(time(NULL));

    at the beginning of your main function.

    Only use it once

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yes.

    Only call srand() once for the whole application!

    Each time a die is created te random generator is re-initialized, which gives the same random numbers for every die, as long as they are created and used within the same microsecond (which, in the context, is a long time).

    Call srand(time(0)); at the beginning of main() and never again!

    Or, perhaps better, use this function:
    Code:
    void init_rand()
    {
      static bool is_initialized = false;
      if (not is_initialized)
        srand(time(0));
      is_initialized = true;
    }
    (edit)To slow... to slow..(/edit)
    Last edited by Sang-drax; 10-29-2002 at 05:45 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by Trauts
    srand(time(NULL));
    Seriously... NULL works just as well as 0 for what he's doing.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    srand((unsigned)time(NULL));

    I can't believe it was as simple as that! Thank you both!

    I had srand((unsigned)time(NULL));, but it was in the Die class. Moving it to main() did the trick.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    you're welcome, glad to be of help

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yes, NULL works just as well, because:
    windows.h
    #define NULL 0
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    never looked in windows.h...

    http://cboard.cprogramming.com/showt...threadid=27536

    Can you please try to help me there? You made one post and didn't explain it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 09:08 PM
  2. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 10:20 AM
  3. Major Problems with GetSaveFileName() and GetOpenFileName()
    By CodeHacker in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2004, 11:05 AM
  4. creating new sets
    By axon in forum C++ Programming
    Replies: 7
    Last Post: 12-03-2003, 05:37 PM
  5. Random Number!
    By Perica in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2002, 05:33 AM

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