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;
}