# Psuedorandom numbers

Well I have been studying psuedorandom sequences for Microchip microcontrollers based on seeds and so far the fastest I have found is thusly from mondo technology:

``````chop    movf    val1,w     ;get the 1st
movwf   val2       ;store the 2nd
movwf   val3       ;back in 3rd
swapf   val1       ;swap nibbles
movf    val1,w     ;return one byte
ret
``````

It looks to be 10 instruction cycles long. However, I would like to see some other methods as I have found that this particular algorithm tends to repeat numbers after a short while with only a 24-bit seed. Does anyone else know of some psuedorandom sequences (C, ASM, or Psuedocode please)?

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.

use a linear congruential generator - probably the best in terms of ease of use to simplicity ratio. It’s on wikipedia
-Salik