xref: /freebsd/tools/test/devrandom/stat.8bit (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1d82e2864SMark Murray#!/usr/bin/perl
2d82e2864SMark Murray
3d82e2864SMark Murray#
4d82e2864SMark Murray# Perform primitive binning into 8-bit bins (take 8 bits of randomness
5d82e2864SMark Murray# at a time) and see if the distribution is flat. The output should be
6d82e2864SMark Murray# checked by eye - are all the numbers roughly the same?
7d82e2864SMark Murray#
8d82e2864SMark Murray# Redirect the output from this to a file - and make a cup of coffee while
9d82e2864SMark Murray# it runs. This program is a CPU Hog!
10d82e2864SMark Murray#
11d82e2864SMark Murray#
12d82e2864SMark Murray
13d82e2864SMark Murrayfor ($i = 0; $i < (1024*32); $i++) {
14d82e2864SMark Murray	open(BIN, "/dev/urandom") || die "Cannot open /dev/urandom - $!\n";
15d82e2864SMark Murray	$len = sysread(BIN, $a, 256);
16d82e2864SMark Murray	close(BIN);
17d82e2864SMark Murray	if ($len > 0) {
18d82e2864SMark Murray		for ($j = 0; $j < $len; $j++) {
19d82e2864SMark Murray			$k = unpack("C", substr($a, $j, 1));
20d82e2864SMark Murray			$bin[$k]++;
21d82e2864SMark Murray		}
22d82e2864SMark Murray	}
23d82e2864SMark Murray}
24d82e2864SMark Murray
25d82e2864SMark Murrayfor ($i = 0; $i < 256; $i++) {
26d82e2864SMark Murray	printf("%.2X ", $bin[$i]);
27d82e2864SMark Murray}
28d82e2864SMark Murrayprintf "\n";
29