xref: /freebsd/tools/test/devrandom/stat.8bit (revision d82e286489da73321a47e329d98a98817b0438b6)
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# $Id$
12d82e2864SMark Murray#
13d82e2864SMark Murray
14d82e2864SMark Murrayfor ($i = 0; $i < (1024*32); $i++) {
15d82e2864SMark Murray	open(BIN, "/dev/urandom") || die "Cannot open /dev/urandom - $!\n";
16d82e2864SMark Murray	$len = sysread(BIN, $a, 256);
17d82e2864SMark Murray	close(BIN);
18d82e2864SMark Murray	if ($len > 0) {
19d82e2864SMark Murray		for ($j = 0; $j < $len; $j++) {
20d82e2864SMark Murray			$k = unpack("C", substr($a, $j, 1));
21d82e2864SMark Murray			$bin[$k]++;
22d82e2864SMark Murray		}
23d82e2864SMark Murray	}
24d82e2864SMark Murray}
25d82e2864SMark Murray
26d82e2864SMark Murrayfor ($i = 0; $i < 256; $i++) {
27d82e2864SMark Murray	printf("%.2X ", $bin[$i]);
28d82e2864SMark Murray}
29d82e2864SMark Murrayprintf "\n";
30