Skip to content

Instantly share code, notes, and snippets.

@thentenaar
Created November 25, 2012 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thentenaar/4143575 to your computer and use it in GitHub Desktop.
Save thentenaar/4143575 to your computer and use it in GitHub Desktop.
C Implementation of the TI's GPL RAND function
static uint16_t seed;
/* RAND: Generate an 8-bit pseudo-random number */
uint8_t gpl_rand(uint8_t divisor) {
/* Update the seed */
seed = ((uint32_t)(seed * 0x6fe5) & 0xffff) + 0x7ab9;
/* Swap bytes, and compute modulus */
return ((((seed & 0xff00) >> 8 ) | ((seed & 0x00ff) << 8 )) % (!divisor ? 1 : divisor));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment