lundi 28 mars 2016

How to generate random numbers in two different arrays?(C++) [duplicate]

This question already has an answer here:

i'm trying to generate different numbers in two different arrays and for some reason the numbers in the two arrays are the same.

this is my code:

#define SIZE 5

void main(){

int* num1;
int* num2;

num1 = new int[SIZE];
num2 = new int[SIZE];

random(num1);
random(num2);
int i;
for (i = 0; i < SIZE; i++)
    cout << num1[i];

cout << endl;

for (i = 0; i < SIZE; i++)
    cout << num2[i];}

and the random function :

void random(int* num){

int i;

time_t t;
srand((unsigned)time(&t));

for (i = 0; i < SIZE; i++ )
    num[i] = (rand() % MAX + 1);}

the ouput always will be the same 5-digit number in the two array (for example array 1 will be 29384 and array 2 also 29384)

any idea how can I solve it?

thanks.




Aucun commentaire:

Enregistrer un commentaire