lundi 6 avril 2015

C - rand() is consistent across multiple function calls

When I try to generate 2 random numbers from within a function call, I'm getting repeated results.


However, the rand function works fine in loops or whatnot provided they are outside of function calls.


I'm guessing that it is because of the system time and that program is small. But I do not know how else to vary the seed.



#include <stdio.h>
#include <ctype.h>
#include <string.h>

int test1(){

int randX, randY;
int range = 5;

srand( time(NULL) );

randX = (rand() % range) + 1;

randY = (rand() % 15 ) + 1;
printf("X:%d Y:%d\n", randX, randY);
}

int main(){

test1();
test1();
test1();
test1();
test1();
}




Aucun commentaire:

Enregistrer un commentaire