PDA

View Full Version : How to make random generated numbers?



EvoloZz
28th November 2012, 18:31
Its kinda annoying when rand() function always gives 41 as a random number, and if I for example have
while(i<5) (list of 5 random numbers), the numbers are always: 41, 18467, 6334, 26500, 19169. Here is the whole thing:
int i=0;

while(i<5){
printf("%d\n", rand());
i++;
}
So I want the program to truly generate random numbers not same numbers everytime -.-

Thanks for help already.

~EvoloZz

kung foo man
28th November 2012, 19:11
Just call srand() with some timestamp first:




#include <time.h>



srand ( time(NULL) );


srand() is "mixing" the numbers, so they wont be the same next start.