double Random::nextDbl()
{
double temp;
if(a >= nintey) // a is an int set in the header under private;
{
a = 0;
shuffle():
{
temp = (double)vect[a];
a++;
return temp;
}
I suppose that would probably be a bit more efficient than my two step method, but as I was trying to figure out how to do it, it made it easier for me to figure out doing it in two steps.
const int bigInt = 250;//I put these values in Global scope for use in main, but if they are just needed for Random.cpp,
const int smallInt = 225;//They would be placed in the private section of Random
class Random
{
private:
double _min, _max;
int currIndex;//keeps check of how far in the vector we are
std::vector<double> vect1;
void fillVect(double min, double max);//Generate random doubles in whatever range is specified
void shuffle();//Shuffle the numbers around in fillVect();
public:
//Constructors
Random();//Default constructor
Random(double min, double max);//Generates random numbers between min and max
Random(int seed);//Seed the random number generator
//Functions
int nextInt();//Returns the next random number as an int
double nextDbl();//Returns the next random number as a primitive double
void setRange(double min, double max);//Sets the range and recreates random numbers
};
#endif
//Constructors
Random::Random(){//Default constructor
srand(unsigned(time(0)));
fillVect(0.0, RAND_MAX);
}
Random::Random(double min, double max){//Generates random numbers between min and max
srand(unsigned(time(0)));
fillVect(min, max);
}
Random::Random(int seed){//Seed the random number generator
srand(seed);
fillVect(0.0, RAND_MAX);
}
//Private Functions
void Random::fillVect(double min, double max){//Generate random doubles in whatever range is specified
vect1.clear();
currIndex = 0;
for (unsigned i = 0; i < bigInt; ++i)
{
double r = (((double) rand() / (double) RAND_MAX * (max - min)) + min);
vect1.push_back(r);
}
shuffle();
}
void Random::shuffle(){//Shuffle the numbers around in fillVect()
random_shuffle(vect1.begin(), vect1.end());
}
//Public Functions
int Random::nextInt(){//Returns the next random number as an int
if (currIndex > smallInt)
{
currIndex = 0;
shuffle();
}
return (int) vect1[currIndex++];
}
double Random::nextDbl(){//Returns the next random number as a primitive double
if (currIndex > smallInt)
{
currIndex = 0;
shuffle();
}
return vect1[currIndex++];
}
void Random::setRange(double min, double max){//Sets the range and recreates random numbers
vect1.clear();//This same function is called in fillVect, but I called again for flexibility incase it was
//removed from fillVect
fillVect(min, max);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
main.cpp //really only used for testing code...
#include "Random.h"
using namespace std;
int main()
{
Random a;
int i;
double min = 0.0000;
double max = RAND_MAX;
cout << "Hello, welcome to the random number generator." << endl << endl
<< "1. To enter a range of values you want to be randomly generated " << endl
<< "2. To use the default values of 0.0 - RAND_MAX " << endl << endl
<< "*Note* The value of RAND_MAX varies from machine to machine" << endl
<< "For this machine it is " << RAND_MAX << endl;
cin >> i;
if(i == 1)
{
cout << "Please enter the values" << endl;
cin >> min >> max;
cout << endl;
}
a.setRange(min, max);
for(int i = 1; i <= bigInt;)
{
cout << i << ". " << a.nextDbl() << endl;
i++;
cout << i << ". " << a.nextInt() << endl;
i++;
}
I'm guessing your the one in the forums that said he has the best menu. Mine was far simpler. I did things a little different, but close to what you did I suppose...
#ifndef _RANDOM
#define _RANDOM
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <iterator>
usingnamespace std;
constint maxNum = 250;
constint ninteyPer = 225;
class Random
{
private:
vector <double> myVect;
void fillVect(double min, double max);
void shuffle();
int x; // this is all basically what I saw being asked for in the assignment. I debated putting this int here, but it seemed like this is where it should be.
public:
Random(); //- Default constructor
Random(double min, double max); //- generates random numbers between min and max.
Random(int seed); //- seed the random number generator
int nextInt(); //- Returns the next random number as an int
double nextDbl(); //- Returns the next random number as a primitive double
void setRange(double min, double max); //- Sets the range and recreates random numbers
};
#endif
#include "random.h"
Random::Random()
{
srand(unsigned(time(NULL)));
fillVect(0.0, RAND_MAX) ;
}
Random::Random(int seed)
{
srand(seed) ;
fillVect(0.0, RAND_MAX) ;
}
Random::Random(double min, double max)
{
srand(unsigned(time(NULL)));
setRange(min, max);
}
void Random::setRange(double min, double max)
{
fillVect(min,max);
shuffle();
}
void Random::fillVect(double min, double max)
{
myVect.clear();
for (unsigned i = 0; i < maxNum; ++i)
{
double r = (((double) rand() / (double) RAND_MAX) * (max - min)) + min;
myVect.push_back(r);
}
shuffle();
}
void Random::shuffle() // seemed like a good way to shuffle the hell out of the vector
{
double dub;
int intr;
srand(unsigned(time(NULL)));
for(int i = 0; i < ninteyPer; i++)
{
intr = rand()%225;
dub = myVect[i];
myVect[i] = myVect[intr];
myVect[intr] = dub;
}
}
int Random::nextInt() // these are probably a bit winded, but were easier for me to wrap my head around
{
int tmpI;
if(x < ninteyPer)
{
tmpI = (int)myVect[x];
x++;
return tmpI;
}
else
{
x = 0;
shuffle();
tmpI = (int)myVect[x];
x++;
return tmpI;
}
}
double Random::nextDbl()
{
double tmpD;
if(x < ninteyPer)
{
tmpD = (double)myVect[x];
x++;
return tmpD;
}
else
{
x = 0;
shuffle();
tmpD = (double)myVect[x];
x++;
return tmpD;
}
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include "random.h"
int main()
{
Random xx(1,RAND_MAX);
Random x;
for(int i = 0; i < 500; i++)
{
cout<< xx.nextDbl()<<endl;
cout<< x.nextInt()<<endl;
}
//obviously far less complicated than what you had in your main derekroolz,
//but it does the testing of the numbers, and simple is always my preferred way to go.
};
Good luck on the grade, I'll be waiting with baited breath till he submits them.
I received a perfect score, this lab turned out to be a lot more simple than I imagined. But definitely was not something you could do without having a fairly deep understanding of c++ so while it may have been a bit of a struggle, I would say congrats to both of us.
Simple? This drove me batty for weeks! Heck, the next assignment, (lab 9), is throwing me for a loop as well over the whole string char thingy. Word to the wise, do not take a six year break between level one and level two classes, because it sucks!