Hello
I have a function that takes in an array as a parameter & puts random numbers into the array. I will run this function over 10 times during the program & I want each time I run the program for a different set of numbers to be in the array.
Currently when I run the function repeatedly, the array is output with the same set of random numbers each time.
Is there a way to get the function to output a different set of random numbers each time the function is called?
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
void random(int array[], int max); // No repeats :D
int main()
{
int a[6];
for (int i = 0; i < 10; i++)
{
srand(time(NULL));
random(a,40);
for (int i = 0; i < 6; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
return 0;
}
void random(int array[], int max) {
int n;
int match = 0; // check if any numbers are repeated
srand(time(NULL));
for (int i = 0; i < 6; i++)
{
n = (rand()%max)+1;
for (int j=0; j<6; j++) {
if (n == array[j]) {
match++;
}
}
if (match==0) {
array[i] = n;
}
else { array[i] = (n+rand()%max); }
match = 0;
}
}