|
Re: Psuedorandom numbers
Quite a bit longer than 10 instruction cycles but here is what our prof made us use for a lab earlier this year:
static unsigned long int SEED_X = 521288629L;
// this is a 32-bit constant
static unsigned long int SEED_Y = 362436069L;
// this is a 32-bit constant
unsigned int random()
{
static unsigned int a = 18000, b = 30903;
SEED_X = a*(SEED_X&65535) + (SEED_X>>16);
// requires 16X16 multiply, 32bit add
SEED_Y = b*(SEED_Y&65535) + (SEED_Y>>16);
// requires 16X16 multiply, 32bit add
return (SEED_X + SEED_Y)/2;
// requires 32bit add, 32bit shift
}
We had to implement this in ASM for our Lab before we started C so I have an ASM implementation, if you want to see it PM me.
__________________
2007 Wisconsin Regional Highest Rookie Seed & Regional Finalists (Thanks 930 & 2039)
2008 MN Regional Semifinalists (Thanks 2472 & 1756)
2009 Northstar Regional Semifinalists (Thanks 171 & 525)
|