Created
September 20, 2024 17:59
-
-
Save notcancername/5f17a6a6ec7a587b1df27edef506fef2 to your computer and use it in GitHub Desktop.
random bit generator in c
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
struct random_bit_generator { | |
uint32_t word; | |
uint8_t bits_remaining; | |
}; | |
static bool next_bit(struct random_bit_generator *rbg) { | |
if (rbg->bits_remaining == 0) { | |
rbg->word = arc4random(); | |
rbg->bits_remaining = 32; | |
} | |
const bool bit = (rbg->word >> (--rbg->bits_remaining)) & 1; | |
return bit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment