Skip to content

Instantly share code, notes, and snippets.

@notcancername
Created September 20, 2024 17:59
Show Gist options
  • Save notcancername/5f17a6a6ec7a587b1df27edef506fef2 to your computer and use it in GitHub Desktop.
Save notcancername/5f17a6a6ec7a587b1df27edef506fef2 to your computer and use it in GitHub Desktop.
random bit generator in c
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