-
Random Numbers
Hey guys, I REALLY need a good random generator, not a seudo random generator that has the SAME combination of numbers every time. I wanted to maybe use the time but I didn't see any functions for converting to an int? So Im stuck with a time_whatever object. Any1 have any ideas for how I can accomplish a good random generator?
-
Perhaps you forgot to seed with srand()? Anyway, you could read up Prelude's article on using rand() and her tutorial on random numbers. She also has a Mersenne Twister library available at her website, which I modified and requested for comment in a rather neglected thread.
-
Thanks a lot, I'll take a look at those links.
-
Unless you're doing some serious encryption or need a very large range of random numbers, the standard rand() should work quite well. As long as you seed the random number generator with srand().
Code:
#include <stdio.h> /* for printf() */
#include <stdlib.h> /* for srand(), rand() */
#include <time.h> /* for time(), time_t */
int main(void) {
srand((unsigned)time(NULL)); /* only call srand() once! */
printf("Your random digit is: %d\n", rand() % 10); /* sorry, Prelude */
return 0;
}
-
Hehe, good thing you added some code to support what you were saying because I was seeding before but I realised that I was contradicting your comment about only call srand() once!