Created
November 25, 2012 13:49
-
-
Save thentenaar/4143575 to your computer and use it in GitHub Desktop.
C Implementation of the TI's GPL RAND function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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