source: Project Repository/src/random_test.cpp @ 1

Revision 1, 1.9 KB checked in by pchapin, 8 years ago (diff)

Added the HWRNG project.

Line 
1/****************************************************************************
2FILE          : random_test.c
3LAST REVISION : 2005-11-21
4SUBJECT       : Random number generator test program.
5PROGRAMMER    : (C) Copyright 2005 by Peter C. Chapin
6
7Although there are RNG test suites out there, I thought it would be
8instructive to write a few tests "manually".
9
10Please send comments or bug reports to
11
12     Peter C. Chapin
13     Electrical and Computer Engineering Technology
14     Vermont Technical College
15     Randolph Center, VT 05061
16     pchapin@ecet.vtc.edu
17****************************************************************************/
18
19#include <iostream>
20#include <iomanip>
21#include "random.h"
22
23int *bucket_array[16];
24
25unsigned current;
26const int mask[16] = {
27  0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF,
28  0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
29};
30
31int main(void)
32{
33  // Set up the generator.
34  rnd::standard_random base_gen;
35  base_gen.time_seed();
36  rnd::bit_random gen(base_gen);
37
38  // Allocate the bucket arrays.
39  int i, size;
40  for (i = 0, size = 2; i < 16; ++i, size *= 2) {
41    bucket_array[i] = new int[size];
42    for (int j = 0; j < size; ++j) bucket_array[i][j] = 0;
43  }
44
45  // Generate random bits and catagorize the results.
46  for (i = 0; i < 1000000; ++i) {
47    current = (current << 1) | gen.next();
48    for (int j = 0; j < 16; ++j) {
49      bucket_array[j][current & mask[j]]++;
50    }
51  }
52
53  // This is just for now. Need to do Chi-squared analysis of results.
54  for (i = 0; i < 2; ++i) {
55    std::cout << std::setw(7) << bucket_array[0][i];
56  }
57  std::cout << "\n";
58  for (i = 0; i < 4; ++i) {
59    std::cout << std::setw(7) << bucket_array[1][i];
60  }
61  std::cout << "\n";
62  for (i = 0; i < 8; ++i) {
63    std::cout << std::setw(7) << bucket_array[2][i];
64  }
65  std::cout << "\n";
66  return 0;
67}
Note: See TracBrowser for help on using the repository browser.