Page 1 of 1
Random Number...
Posted: Tue May 12, 2009 11:38 pm
by Sylus101
I've found (though I wouldn't be adverse to getting another suggestion) about the proper way to generate a random number (with a max value), but to seed the random number generator I'd like to use the current DS Clock time. I can't seem to find any macro or define for it... what am I missing?
Re: Random Number...
Posted: Wed May 13, 2009 2:17 am
by WinterMute
Code: Select all
#include <nds.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv) {
consoleDemoInit();
srand(time(NULL));
printf("random number is %d\n", rand());
while(1) swiWaitForVBlank();
}
Re: Random Number...
Posted: Wed May 13, 2009 3:42 am
by Sylus101
Thanks, that works, lol.
Another quick question... should you only have to call srand() once? What I mean is, afterwards will rand() generate a number seeded by the time at that moment or at the moment srand() was called.
Re: Random Number...
Posted: Wed May 13, 2009 12:03 pm
by vuurrobin
time is just a function that a number. so srand will always use the time it was given when you called time().
however, you only have to call srand() once. when you call rand(), it will change the seed for you, so calling rand() twice should give you 2 different numbers.
Re: Random Number...
Posted: Wed May 27, 2009 11:43 am
by Jell
I ran through this problem lately, actually srand(time(NULL)) did not randomize my rand() calls.
My guess WAS that the function time() returns a pointer, therefore your seed will always be the same (making the whole process useless).
[Edit:] But obviously I was guessing wrong!
Re: Random Number...
Posted: Wed May 27, 2009 2:00 pm
by WinterMute
Actually, srand(time(NULL)) will work fine, just not on most emulators.
Re: Random Number...
Posted: Wed May 27, 2009 2:24 pm
by Jell
Oups that's true! Sorry for the wrong advice...