-
Array question
Hello everyone,
I was just messing around in C++ and decided to make a random number generator to select lottery numbers. It looks like this:
Code:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main(){
int const low = 1;
int const high = 53;
int A=0, B=0, C=0, D=0, E=0 , F=0;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
A = rand() % (high - low + 1) + low;
B = rand() % (high - low + 1) + low;
C = rand() % (high - low + 1) + low;
D = rand() % (high - low + 1) + low;
E = rand() % (high - low + 1) + low;
F = rand() % (high - low + 1) + low;
cout << "Your randomly selected lottery numbers are: " << endl;
cout << A << " " << B << " " << C << " " << D << " " << E << " " << F << endl;
cin.get();
}
I intend to put some Boolean operators in it, so that I don't get any repeating numbers.
It's not perfect, I know, but I was wondering if I could have made this using an array?
-
You could, if you wanted. What you really want is not random number-picking, but a random shuffle, and in that case you might want an array to hold the shuffle.
-
There is an old thread started by yours truly which has a little discussion on methods to achieve this, starting with a basic example by me, then options using the std::set posted by some senior members:
Allocation method
-
Excellent, I'm going to try this out. Thank you both very much.
-
here's one that you can use
Code:
{
using namespace std;
srand(time(NULL));
vector<int> result;
for ( int i = 1 ; i <=50 ; ++i)
result.push_back(i);
random_shuffle(result.begin() , result.end() );
}