lundi 30 mars 2015

Xorshift pseudorandom number generator with shiftvalues in progmem

I started implementing a xor shift generator. To safe some RAM I tried to put the shiftvalues into the PROGMEM but this actually does make some "strange" results and i dont know why. It seems that it returns the same value for a long time, than get back to regular expected results.


I hope you can help me and show me what I have done wrong.



const uint8_t shift[] PROGMEM =
{
0x01, 0x01, 0x02, 0x01, 0x01, 0x03, 0x01, 0x07, 0x03, 0x01, 0x07, 0x06, 0x01, 0x07, 0x07, 0x02, 0x01, 0x01,
0x02, 0x05, 0x05, 0x03, 0x01, 0x01, 0x03, 0x01, 0x05, 0x03, 0x05, 0x04, 0x03, 0x05, 0x05, 0x03, 0x05, 0x07,
0x03, 0x07, 0x01, 0x04, 0x05, 0x03, 0x05, 0x01, 0x03, 0x05, 0x03, 0x06, 0x05, 0x03, 0x07, 0x05, 0x05, 0x02,
0x05, 0x05, 0x03, 0x06, 0x03, 0x05, 0x06, 0x07, 0x01, 0x07, 0x03, 0x05, 0x07, 0x05, 0x03, 0x07, 0x07, 0x01
}; //needed as hex

static uint8_t y8 = 13;
static uint8_t cur_shift = 0, request_count = 0;

uint8_t rnd()
{
//shift the shifting numbers
request_count++;
if(request_count == 255)
{
request_count = 0;
cur_shift++;
if (cur_shift == 24)
{
cur_shift = 0;
}
}
//shift something around
y8 ^= (y8 << pgm_read_byte(&(shift[cur_shift * 3])));
y8 ^= (y8 >> pgm_read_byte(&(shift[cur_shift * 3 + 1])));
return y8 ^= pgm_read_byte(&(shift[cur_shift * 3 + 2]));
}


Reference to the Numbers for the shift





Aucun commentaire:

Enregistrer un commentaire