Sidebar

The XABC Random Number Generator

One time I needed a random number generator. It was for the PIC10, which has something like a few dozen bytes of RAM. I didn't need security, just speed and reasonably long periods sizes.

After much testing, I came up with one that passes most statistical tests. Emphasis on “most”. It's not a very good RNG, but a few people seem to be using it, so I figured I'd better migrate it here.

It has an undefined period size. It's state loops back to the start at different times depending on what the start is. So far I don't think there's been any issues, but there could be.

Are there very short cycles? Maybe! likely not too short though, as there is a fixed counter that is guaranteed not to loop for 256 bytes, and IIRC the testing I did with randomly chosen seeds usually had periods in the hundreds of millions.

This thing is pure trial and error. There's no logic to it, and I wouldn't even begin to understand how to formally analyze it. If you need good fast numbers, use XORShift, or maybe blake2b hash a counter or something if you need even better.

If you need four bytes of state and very few operations, all single 8 byte instructions, and you don't care if there's a chance that it loops endlessly in a very short cycles, and you don't need security, maybe you need XABC! It's named for the 4 bytes of state it carries, in 4 separate variables.

Just don't ask me how it works. I have no clue. It's blazing fast though!

uint8_t x,a,b,c;
uint8_t randRNG8() // Originally unsigned char randomize().
  {
      x++;               //x is incremented every round and is not affected by any other variable
      a = (a^c^x);       //note the mix of addition and XOR
      b = (b+a);         //And the use of very few instructions
      c = ((c+(b>>1))^a);  //the right shift is to ensure that high-order bits from b can affect  
      return( c);         //low order bits of other variables
  }
 

There you have it! You can spend 5+ years on a FOSS automation framework, and a week on a terrible RNG, and find the terrible RNG is actually more useful to more people than most of your more professional projects!

It seems that someone has implemented it in assembly code here: https://rosettacode.org/wiki/Evolutionary_algorithm

tech/the_xabc_random_number_generator.txt · Last modified: 2021/01/14 09:27 by admin