Omnimaga

Calculator Community => TI Calculators => Calculator C => Topic started by: compu on March 04, 2011, 03:27:29 pm

Title: Best way for random number generation?
Post by: compu on March 04, 2011, 03:27:29 pm
Hi, I have to generate random numbers on the Nspire... Till now I have been using this function:
Code: [Select]
int random(int max)
{
    unsigned int *counter = (unsigned int *) 0x9001000C;
    return *counter/(65535/max);
}
(same way as in nTris)

This works fine for me on the emulator, but on real hardware It often gives the same numbers :(
In my case I'm using random(3) for random level generation in Shunledge (whole source here (http://nspforge.unsads.com/p/shunledge), topic here (http://ourl.ca/9169)).. so, what could be a better way to do this?

PS: Yes, I have initialized the timer ;)
Title: Re: Best way for random number generation?
Post by: christop on March 04, 2011, 05:38:04 pm
I just use the classic linear congruential PRNG in one of my programs:
Code: [Select]
unsigned prngseed;

int rand()
{
        prngseed = 1103515245UL*prngseed + 12345UL;
        return (unsigned)prngseed % ((unsigned)INT_MAX+1);
}

void srand(unsigned s)
{
        prngseed = s;
}
(This is a modification of my code so it works with 32-bit ints (assuming int is 32 bits on the Nspire). My program runs on a system with 16-bit ints)

These are actually implementations of the C standard functions rand() and srand(). It works pretty well considering its simplicity. It also doesn't rely on a timer to generate each successive value. You can set the seed using the timer though:
Code: [Select]
srand(*counter);

To use it with a maximum value you can do this:
Code: [Select]
/* RAND_MAX = 2147483647 with 32-bit int */
x = (unsigned long long)rand() * max / RAND_MAX;
Yeah, it uses 64-bit arithmetic (if long long is 64 bits, that is), but it is far more uniformly distributed than using "x/(65535/max)" as you are doing (except when max is an integer factor of 65535, in which case it's the same).
Title: Re: Best way for random number generation?
Post by: AngelFish on March 04, 2011, 06:15:45 pm
Hi, I have to generate random numbers on the Nspire... Till now I have been using this function:
...
 so, what could be a better way to do this?

The *best* way would be Blum Blum Shub. The fastest way would probably be that LCRNG Chris posted.
Title: Re: Best way for random number generation?
Post by: compu on March 05, 2011, 02:47:12 pm
christop, must INT_MAX be 4294967296 then ???
I don't get this to work :banghead:
Title: Re: Best way for random number generation?
Post by: christop on March 05, 2011, 05:24:49 pm
Oh, I forgot to define INT_MAX. It's the maximum value of a signed int, so it's 2147483647 with 32-bit ints. It's the same as RAND_MAX.
Title: Re: Best way for random number generation?
Post by: compu on March 23, 2011, 04:09:11 pm
Finally I found the time to fix my game, the levels are generated much better now.
Thanks for helping me out :)