Thread: Seeding a Random Number Generator

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    Seeding a Random Number Generator

    Hi all,

    I am trying to design a class named "aDie" to roll a die 6,000 times and print out the results. I have a random number generator class that I am using.

    I would like for the method int inputSeed() to accept a user's seed and then pass it to the method void seed(int s) which in turn passes it to the method void setSeed(int s). However, the code I'm using now does not generate different random numbers according to the seed I'm inputting. Each time I enter a different seed I get the same 10 random numbers.

    I know my for loop only runs 10 times...I did it that way to test the random number generation before I move on to other parts of the assignment.

    I'm not exactly sure what I'm doing incorrectly. Can someone please help?
    Any assistance is greatly appreciated.

    My files:

    Code:
    #ifndef ARANDOMNUMBERGENERATOR_H
    #define ARANDOMNUMBERGENERATOR_H
    
    #include <iostream>
    
    using namespace std;
    
    class aRandomNumberGenerator
    {
    public:
    	aRandomNumberGenerator(void);
    	void setSeed (int);//sets seed
                    double generate ();//generates rand num using seed
    	~aRandomNumberGenerator(void);
    private:
                    int seed;//private, modified by setSeed
    };
    
    #endif
    Code:
    #include "aRandomNumberGenerator.h"
    
    #include <stdlib.h>
    
    aRandomNumberGenerator::aRandomNumberGenerator(void)
    {
         seed = 0;
    }
    
    void aRandomNumberGenerator::setSeed (int s) 
    {
         seed = s;
         srand(seed);
    }
    double aRandomNumberGenerator::generate()
    {	 
         return rand();
    }
    
    aRandomNumberGenerator::~aRandomNumberGenerator(void)
    {
    
    }
    Code:
    #ifndef ADIE_H
    #define ADIE_H
    
    #include <iostream>
    
    #include "aRandomNumberGenerator.h" //a RandomNumberGenerator class declaration
    
    using namespace std;
    
    class aDie
    {
    public:
    	aDie(void);
    	int inputSeed();
    	void seed(int s);
    	~aDie(void);
    private:
    	int users_seed_input;
    	int roll_count;
    };
    
    #endif
    Code:
    #include <iostream>
    #include <iomanip>
    
    #include "aDie.h"
    #include "aRandomNumberGenerator.h"
    
    using namespace std;
    
    aDie::aDie(void)
    {
    	
    }
    
    // accepts seed from user
    int aDie::inputSeed()
    {
         cout <<"PLEASE ENTER A SEED FOR THE DIE: ";
         cin >> users_seed_input;
         cout <<"\nYOU ENTERED: " << users_seed_input <<endl; 
         //returns the seed entered by the user
         return 0;
    }
    
    void aDie::seed(int s)
    {
    	users_seed_input = s;
    	aRandomNumberGenerator r;
    	r.setSeed(users_seed_input);
    }
    
    
    aDie::~aDie(void)
    {
    	
    }
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    #include "aDie.h"
    #include "aRandomNumberGenerator.h"
    
    using namespace std;
    
    int main(void)
    {
    	aDie mydie;
    	aRandomNumberGenerator myrng;
    
    	mydie.inputSeed();
    	
    
    	for(double i = 0; i<10; i++) //loop to print the 10 random numbers
    	{
    	cout << myrng.generate() << endl; //print each number on a new line
    	}
    
    	
    
    	
    
    	getchar();
    	getchar();
    
    	return 0;
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I would like for the method int inputSeed() to accept a user's seed and then pass it to the method void seed(int s)
    You should probably do that then.

    Soma

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by girly_engineer View Post

    However, the code I'm using now does not generate different random numbers according to the seed I'm inputting. Each time I enter a different seed I get the same 10 random numbers.
    Considering that you are using the same seed every time you run the program, you find that surpising?

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Code:
    int aDie::inputSeed()
    {
         cout <<"PLEASE ENTER A SEED FOR THE DIE: ";
         cin >> users_seed_input;
         cout <<"\nYOU ENTERED: " << users_seed_input <<endl; 
         //returns the seed entered by the user
         return 0;
    }
    this returns 0 not the "seed entered by user." Did you mean to do.
    Code:
    return users_seed_input;
    Your comment // makes it appear that way, but that confuses me because when you call.
    Code:
    mydie.inputSeed();
    you don't attempt to assign its value to anything else.

    Secondly You don't ever Call
    Code:
    void aDie::seed(int s)
    Which makes it look like S never gets defined a value. If you would change inputSeed() to a void and notice that this already assigns value to users_seed_input. You could Remove the line
    Code:
    users_seed_input = s;
    and take the "int s" out of void aDie::seed(int s). And it seems like it would then work properly. Then when calling mydie.
    Code:
    mydie.inputSeed();
    mydie.seed(); 
    
    for(double i = 0; i<10; i++) //loop to print the 10 random numbers
    {
      cout << myrng.generate() << endl; //print each number on a new line
    }
    Or you could leave it as a int function and when you call it try.
    Code:
    mydie.seed(mydie.inputSeed());
    I'm still a beginner programmer too so I might have missed something. Hope this helps ;o).

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    And I did miss something! You seed the randomgerator class R (even though you didn't actually seed this class), but had you done so correctly you are still calling a different version of the class during your main.... which has not been seeded. . you might want to prevent calling a class during your other class function and just use logic of your class fuctions to call it during main.
    Code:
    myrng.setSeed(mydie.inputSeed());
    that would also make your aDie::Seed function useless and you could delete it.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    As demonstrated, people will provide varying degrees of help, often in line with how much effort they think you've put in to begin with. Some are getting the impression you could probably easily solve this on your own through acquiring a little more practice amd skill.

    What you need to do is to work out how to use a debugger so that you can help yourself. What is your IDE / compiler / Operating System?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Just a note: srand() and rand() can only generate single streams of random numbers.

    This means when you create another instance of that random number generator class, it's going to mess up the instance you created earlier!

    If you're interested in generating multiple discrete streams of random numbers I recommend making a class using the Mersenne twister random number generator: Mersenne twister - Wikipedia, the free encyclopedia

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number generator
    By rehan in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2008, 02:14 AM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 06:38 AM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM