-
Notifications
You must be signed in to change notification settings - Fork 11
Open
Description
fast-loaded-dice-roller/src/c/flip.c
Lines 14 to 25 in 19d30ab
| static int k = 30; | |
| static int flip_word = 0; | |
| static int flip_pos = 0; | |
| int flip(void){ | |
| if (flip_pos == 0) { | |
| flip_word = rand(); | |
| flip_pos = k; | |
| } | |
| --flip_pos; | |
| return (flip_word >> flip_pos) & 1; | |
| } |
rand() returns a random 31-bit integer packed into a 32-bit integer (whose sign bit is always zero). The positions of the extracted bits should then be 0, ..., 30. However, by setting k=30, flip_pos = k, and flip_pos --, we only ever extract bits the bits at locations 0, ..., 29.
Also the code can be made more general to determine k automatically from limits.h and RAND_MAX.
Metadata
Metadata
Assignees
Labels
No labels