RNGA - Random Number Generator

Status
Not open for further replies.
Thankyou. Looks interesting, I'll give it a try. Glad you found register addresses that actually worked :) !

I did some work a few years back on testing for randomness, and came to some interesting conclusions. One idea was to treat consecutive RNs in a file as pairs and to plot them as black points on a white canvas. Eventually, as you plot more and more points, you should see the canvas go completely black... and the number of points to make it so is predictable. Comparing the actual to the theoretical gives a measure of how good the RNs are distributed.

Also, well before the canvas changes colour completely, you can see a scatter diagram forming, which again should be evenly distributed (in general). Fascinating to see some patterns made by non random files of numbers.

I wrote a windows based utility to perform this plotting (and investigate other statistics on a file of RNs). Happy to share if anyone is interested...
 
Interesting (though I don't usually run windows). I usually test the RNGs with the NIST suite, Diehard, ENT, ... and test the speed.
https://github.com/manitou48/DUEZoo/blob/master/RNGperf.txt

Re: visualizing randomness
In a security class i used to teach, I would show the ineffectiveness of ECB encryption on an image (vs using CBC)
Tux.jpgTux_ecb.jpgTux_secure.jpg

updated statistical data on T3.5/3.6 random number generator: https://forum.pjrc.com/threads/48745-Teensy-3-6-Random-Number-Generator
 
Last edited:
Had time now to try out your code, and with your indulgence I made a few modifications to spit out RNs as fast as possible to the serial monitor. I then collected these in a text file (taken directly from the serial monitor with copy/paste) and processed it externally to create a true hex data file, which I could then import into my Windows Utility (InspectorPlus) and create a scatter diagram (shown below). The diagram was halted after plotting 229,376 bytes (in x,y consecutive pairs) of a 1,865,416 total file size. It illustrates the distribution much as your own right-hand picture section shows. It would need a file of approx 7 MBytes to completely black out, but the picture shows the general idea.

Thanks for your help... an enjoyable exercise.

RN002.jpg

Code:
// T3.6/T3.5 Random Number Generator
//==================================
//Thanks and acknowledgement to "manitou" for original test code
//Modifications by "TelephoneBill" dated 23 FEB 2017 
#define RNG_CR_GO_MASK          0x1u
#define RNG_CR_HA_MASK          0x2u
#define RNG_CR_INTM_MASK        0x4u
#define RNG_CR_CLRI_MASK        0x8u
#define RNG_CR_SLP_MASK         0x10u
#define RNG_SR_OREG_LVL_MASK    0xFF00u
#define RNG_SR_OREG_LVL_SHIFT   8
#define RNG_SR_OREG_LVL(x)      (((uint32_t)(((uint32_t)(x))<<RNG_SR_OREG_LVL_SHIFT))&RNG_SR_OREG_LVL_MASK)
#define SIM_SCGC6_RNGA          ((uint32_t)0x00000200)

uint32_t GenRandNum(){
  RNG_CR |= RNG_CR_GO_MASK;
  while((RNG_SR & RNG_SR_OREG_LVL(0xF)) == 0);  //wait for RN to be generated
  return RNG_OR;                                //return RN
}

void setup() {
  Serial.begin(230400);
  SIM_SCGC6 |= SIM_SCGC6_RNGA;                  //enable RNG
  RNG_CR &= ~RNG_CR_SLP_MASK;
  RNG_CR |= RNG_CR_HA_MASK;                     //high assurance, not needed
}

void loop() {
  uint32_t rn;
  String strRN, prtRN;
  rn=GenRandNum();                              //generate RN
  strRN = "00000000" + String(rn, HEX);         //string with leading zeroes
  prtRN = strRN.substring(strRN.length()-8);    //truncate to print 8 characters
  Serial.println(prtRN);                        //output to serial monitor
}
 
Status
Not open for further replies.
Back
Top