Post on 03-Jun-2018
8/11/2019 Presentation Somo
1/17
Random Number Generator
(RNG) for Microcontrollers
Dr. S. Somokanta Singh
Manipur Institute of Management Studiessansom68@gmail.com
8/11/2019 Presentation Somo
2/17
Dr. S. Somokanta SinghMIMS, M.U.
Introduction
RNG Wisdom
Motivation for an RNG
Other RNG Methods
Requirements for a Good RNG
Approaches Taken
Testing
Implementation
Summary
8/11/2019 Presentation Somo
3/17
Dr. S. Somokanta SinghMIMS, M.U.
RNG Wisdom
Anyone who considers arithmeticalmethods of producing random dig i ts is ,
of course, in a state of sin. John von
Neumann (1903-1957).
The generation of random numbers is
too important to be left to chance.Robert R. Coveyou, Oak Ridge NationalLaboratory in Tennessee.
8/11/2019 Presentation Somo
4/17
Dr. S. Somokanta SinghMIMS, M.U.
Motivation For An RNG
For Microcontrollers More Interesting Robotic Behavior
Avoid Stuck-In-A-Corner Logic
Used In AI Techniques Genetic Algorithms
Neural Networks
Fuzzy Logic
8/11/2019 Presentation Somo
5/17
Dr. S. Somokanta SinghMIMS, M.U.
Other RNG Methods
Hardware-Based (true) RNGs
White-Noise Source
Transistor Circuits
Our desire is to have a pseudo random
number generator, as defined by
8/11/2019 Presentation Somo
6/17
Dr. S. Somokanta SinghMIMS, M.U.
Requirements - a Good RNG
Key Requirements (detailed list in paper).
N = F(), where
N is in the range 0 to 255
Large cycle of Ns before pattern repeats. All values of N are generated the same number of
times in the cycle.
Windows of consecutive Ns in cycle meet
Constraints for Average, Min, and Max values.
Small Code Size
8/11/2019 Presentation Somo
7/17
Dr. S. Somokanta SinghMIMS, M.U.
Approaches Taken
Table-Based Method
Define a table of 128 values.
Use EXOR to generate 128 other values.
Cycle through these 256 values 64
different ways.
Cycle length of 16,384.
Met the goodness criteria.
62 bytes of code + 128 bytes for table =
190 total bytes. 4 bytes of RAM.
8/11/2019 Presentation Somo
8/17
Dr. S. Somokanta SinghMIMS, M.U.
Approaches Taken
Equation-Based Method
2-byte seed value in RAM
seed = 181 * seed + 359
Return top 8 bits of seed.
Cycle length of 65,536.
Met the goodness criteria.
25 bytes of code, 4 bytes of RAM.
8/11/2019 Presentation Somo
9/17
Dr. S. Somokanta SinghMIMS, M.U.
Approaches Taken
Notes:
181 and 359 determined by a program searching
for values to meet the goodness criteria.
Table method also used a program to find right
128 table values.
8/11/2019 Presentation Somo
10/17
Dr. S. Somokanta SinghMIMS, M.U.
Generated Numbers
First 240 numbers:1 255 117 4 73 222 125 232 15 167 21 110 230 252 49 2735 65 133 50 218 156 132 185 223 239 99 114 197 223 22 226226 208 81 76 71 229 135 182 203 4 237 226 1 207 119 8560 170 216 82 145 187 134 223 211 228 179 190 152 202 84 116
50 209 28 68 182 28 128 52 248 145 181 6 139 210 172 63196 68 28 34 183 10 119 181 56 9 242 186 219 229 129 182243 2 216 237 149 132 106 98 148 78 108 218 134 5 210 217189 13 80 163 78 137 89 59 13 94 34 102 141 48 159 16835 99 131 69 227 27 68 64 161 59 20 94 241 104 232 3538 6 115 211 85 56 43 113 81 228 66 194 176 172 172 74196 244 31 77 162 226 13 206 30 88 171 146 203 251 237 29
254 47 135 179 203 23 236 87 6 153 81 206 66 87 170 156213 181 171 5 209 217 199 12 10 165 51 118 22 190 227 19971 136 138 67 178 39 158 237 43 126 81 138 69 50 152 15885 167 38 109 111 0 113 250 103 34 171 11 208 177 201 33
8/11/2019 Presentation Somo
11/17
Dr. S. Somokanta SinghMIMS, M.U.
Testing The Numbers
Met The Defined Goodness Criteria
Inspection
Graphical Plots Plot number pairs on 256x256 grid.
8/11/2019 Presentation Somo
12/17
Dr. S. Somokanta SinghMIMS, M.U.
Testing
1024 pairs
plotted
8/11/2019 Presentation Somo
13/17
Dr. S. Somokanta SinghMIMS, M.U.
Testing
Half the
pairs plotted
8/11/2019 Presentation Somo
14/17
Dr. S. Somokanta SinghMIMS, M.U.
Testing
All pairs
plotted
8/11/2019 Presentation Somo
15/17
Dr. S. Somokanta SinghMIMS, M.U.
Testing
All pairs
with another
set of
constants
8/11/2019 Presentation Somo
16/17
Dr. S. Somokanta SinghMIMS, M.U.
Implementation
25 Bytes of 68HC11 CodeRandom:
PSHB ; (1,3) Remember the current value of B* scratch = seed * multiplier
LDAA #MULTIPLIER ; (2,2) A = #181LDAB SEED_LOW ; (2,3) B = the low byte of the seed
MUL ; (1,10) D = A x BSTD RandomScratch ; (1,4) scratch = D
LDAA #MULTIPLIER ; (2,2) A = #181LDAB SEED_HIGH ; (2,3) B = the high byte of the seed
MUL ; (1,10) D = A x B* low byte of MUL result is added to the high byte of scratch
ADDB RandomScratch ; (2,3) B = B + scratch_highSTAB RandomScratch ; (2,3) scratch = seed * 181
*LDD RandomScratch ; (2,4) D = scratch
ADDD #ADDER ; (3,4) D = D + 359STD RandomSeed ; (2,4) remember new seed value
* (A = SEED_HIGH from ADDD instruction)PULB ; (1,4) Restore the value of BRTS ; (1,5) A holds the new 8-bit random number
8/11/2019 Presentation Somo
17/17
Dr. S. Somokanta SinghMIMS, M.U.
Summary
RNG Studied
RNG Goodness Criteria Developed
Two RNG Methods Developed Both Methods Were Critiqued
Equation-Based RNG Chosen For
Number Coverage and Code Size 68HC11 Code Implemented and
Tested