I'd like to use the built in hardware random number generator in my RPI3 for a project. Currently I'm only able to use /dev/hwrng to save binary dumps with
dd if=/dev/hwrng of=data.bin bs=25 count=1
What I need for my project is to read 200 bit long data chunks from the random source (/dev/hwrng) with a frequency of 1 reading/second and count the 1's in it and write the result as decimal into a text file with a timestamp, like this:
datetime, value
11/20/2018 12:48:09, 105
11/20/2018 12:48:10, 103
11/20/2018 12:48:11, 97
The decimal number should be always close to 100, since it is a random data source and the expected number of 1's and 0's should be the same. Any help is appreciated....
I did come up wit a perl script that is close to what I wan't, so let me share it. I'm sure it could be done in a much cleaner way though...
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
my @bitcounts = (
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3,
3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,
3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2,
2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5,
5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3,
2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4,
4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4,
4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,
5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5,
5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
);
for (my $i=0; $i <= 10; $i++) {
system("dd if=/dev/hwrng of=temprnd.bin bs=25 count=1 status=none");
my $filename = 'temprnd.bin';
open(my $fh, '<', $filename) or die "$!";
binmode $fh;
my $count = 0;
my $byte = 0;
while ( read $fh, $byte, 1 ) {
$count += $bitcounts[ord($byte)];
}
my $dt = DateTime->now;
print join ',', $dt->ymd, $dt->hms,"$count\n";
system("rm temprnd.bin");
sleep 1;
}
__END__
Try running the following code
If you want to save it to a file, add simple redirect in the end
> somefileUpdating on the new request, try running the following code