added main file.
This commit is contained in:
parent
515c815f63
commit
392ab6f796
1 changed files with 39 additions and 0 deletions
39
randombyte128.c
Normal file
39
randombyte128.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
long long millis(){
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
long long millisecs = (long long)tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
||||
return millisecs;
|
||||
}
|
||||
bool flip(){
|
||||
bool n = false;
|
||||
long long then = millis() +1;
|
||||
while (millis() <= then){
|
||||
n=!n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
bool get_fair_bit(){
|
||||
while (true) {
|
||||
bool a = flip();
|
||||
if (a != flip()){
|
||||
return a;
|
||||
}
|
||||
}
|
||||
}
|
||||
char get_random_byte(){
|
||||
char n = 0;
|
||||
for (int i = 0; i < 8; ++i){
|
||||
n <<=1;
|
||||
n|= get_fair_bit();
|
||||
}
|
||||
return n;
|
||||
}
|
||||
int main(){
|
||||
for (int i = 0; i < 128; ++i){
|
||||
printf("%hd\n", get_random_byte());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue