Path:
Home =>
AVR-EN =>
Applications =>
Random ATtiny13 => 16-bit
Diese Seite in Deutsch:
 |
AVR applications
Random number generation with ATtiny13
Application and software for 16-bit random numbers in assembler |
16-bit random numbers with ATtiny13
Just like 8-bit randoms also 16-bit randoms can be generated. I use the following
algorithm:
.equ cSeed = 12345
.equ cRnd = 0b1011101110101010
.equ cCnt = 1000000
;
; Start values
ldi ZH,High(cSeed)
ldi ZL,Low(cSeed)
; Set randomizer
ldi YH,High(cRnd)
ldi YL,Low(cRnd)
; Prepare a random
eor ZH,YL
eor ZL,YH
add ZL,YL
adc ZH,YH
adiw YL,1
Loop:
rjmp Loop
We start with two 16-bit numbers: cSeed and cRnd. And those two
are crumbled a little bit with two EXOR and an ADIW instruction. Please note
that the EXOR mixes MSB and LSB a little bit, that proved to be more successful
than an orderly use.
For the so-produced randoms I have modified the AVR Assembler Simulator avr_sim
slightly. In version 2.5 of avr_sim
I added code that writes simulated content in registers, ports and in the SRAM
as tab-separated values to a .CSV file, either in hexadecimal or in decimal format.
This random generator then doesn't need an RS232 interface like in the case
here. It even works without an ATtiny13,
only with a simulated one.
With this equipment I have generated 500,000 randoms and wrote those to a
file. If you need such a file: here is it, it
has 3.9 MB full of hexadecimals.
Analysis of 16-bit randoms
You can open such monster files with LibreOffice-Calc, if you like. The
spread-sheet opens and imports it (make sure you add the ASCII-Tab as separator).
But you will not get happy when handling one million randoms with LibreOffice-Calc:
the program sometimes needs several minutes to come with results, and often
crashes.
Whenever software crashes, I create my own. So I wrote my own random generator
analysis program. Of course in Lazarus-Pascal. I feed the .csv file to that and
the program does the following:
- It reads the first line and checks if this line starts with the word
"Comment:". If this is the case it reads this comment.
- In the following line the program reads the column headers and counts
those. The headers are copied to the drop-down field
"Read from column". The last column is selected as input. If
you like, you can select other columns for analysis.
.
- For the selected column it is checked whether the values in this column
are in decimal or in hexadecimal format. The result of this check can be
seen from the respective checkbox. If you change the format with the
checkbox, you might run into several error messages.
- Now Random16 reads all numbers and counts how often the 65,536 different
numbers occur in that column. The maximum number of occurrences (36 per
1 million values) is written to the Read-Only edit field "Max".
- The number of occurrences of the 65,536 numbers is displayed in the
picture on the left side of the window. The horizontal axis are the
256 LSBs, the vertical axis the 256 MSBs. The colors used are
displayed on the color scale. How often the colors are used can be seen
from the number of occurrences right to the color numbers. The largest
number of occurrences are around 15, which is the average of 1,000,000 /
65,536. The colors in the picture show no regularity, the randomness
seems rather fine.
- The picture can be saved as .PNG file if you press the button
"Save as .PNG file".
- To test the randomness even further, the group
"Find a repetition" offers more detailled analysis tools:
- the drop-down field "Select highest" has the 10 most often
occurring numbers, if you select one of it, its value is copied to the
edit field "Search for".
- In the edit field "Search for" numbers can be inputted and
are searched for in the complete value set. If your values are
hexadecimal enclose those in "". All occurrences of this
value are listed in the memo field below, its position and associated
with the three values that follow this occurrence, separated with an
ambersand character.
- If you push the button "Start", all numbers between 0 and
65,535 are searched for re-occurrences. In large value sets this lasts
rather long, the progress bar moves rather slow. It can take up to an
hour until all occurrences have been identified and checked with all
other occurrences.
If any repetition of a quadruplett is found, the further analysis stops
and displays the values there.
- If you press the button "Save ColCnt", you'll get the colors
of the color scale and all occurrences written to a .csv file.
That also looks rather like random.
Lazarus source code of Random16
The Random16 software is written in Lazarus and can be compiled with a Lazarus
Compiler for the sources here.
Assembler source code for generating 1 million randoms
This asm source code produces
1 million randoms. Collection and storage of the generated randoms works under
avr_sim versions from 2.5 upward.
Praise, error reports, scolding and spam please via the
comment page
to me.
©2021 by http://www.avr-asm-tutorial.net