Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // rngtest < ./rand
- // ./rand | dieharder -B -g 200 -a
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <math.h>
- int main() {
- //starting number and table indices
- uint8_t num = 43;
- uint8_t a=131;
- uint8_t b=73;
- //bit width
- const uint8_t bw = sizeof(num)*8;
- //tables of random numbers
- uint8_t atab[] = {
- 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53
- };
- uint8_t btab[] = {
- 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131
- };
- //number of elements in the tables (must be power of two that we can avoid expensive modulus)
- const uint8_t elems=sizeof(atab)/sizeof(atab[0]);
- while(1){
- a = (a+num&(elems-1)) & (elems-1); //use 'num' to set a index
- b = (b+1 ) & (elems-1); //b index increments by 1 and loops
- //swap elements
- uint8_t buf = atab[a];
- atab[a]=btab[b]+num; //add num for additional mixing
- btab[b]=buf;
- uint8_t shift = a&(bw-1); //right shift amount
- num = (num>>shift) | (num<<(bw-shift)); //ror
- num = num ^ atab[a] + btab[b]; //mixing
- fwrite(&num, sizeof(uint8_t), 1, stdout); //write
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement