Random numbers and Vectors

Pages: 12
You mean something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.
Last edited on
Now that the assignment date has been completed, I suppose I could post my code and will update later on the grade I received.

///////////////////////////////////////////////////////////////////////////////////////////////////
Random.h

#ifndef _RANDOM
#define _RANDOM

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>

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

///////////////////////////////////////////////////////////////////////////////////////////////////
Random.cpp

#include "Random.h"

using namespace std;

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

return 0;
}
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...

random.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef _RANDOM
#define _RANDOM
 #include <iostream>
 #include <vector>
 #include <cstdlib>
 #include <ctime>
 #include <algorithm>
 #include <iterator>
 
using namespace std;

const int maxNum = 250;
const int 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 


random.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#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.
Perfect score, so I got lucky, because I struggled with this one.
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!
Topic archived. No new replies allowed.
Pages: 12