True random generator

lazarus

New Member
Hi,
I would like to create a true random generator using a picaxe.

The idea is that the picaxe gets some unpredicatble data from the outside world. and sends it, after proccessing, to the serout pin.

Has anyone done this before?

I would like to use 2 NOT ports from a 7414 to create 2 inpuls generators. I'll then feed the 2 outputs to 2 input pins of a picaxe and to a XOR of the 2 values. To get a random number out of this I would read in 8 bits from the XOR and XOR it again with the previous random number to get the new one.

Do you think this is something that can work?

It should not be possible to calculate the next value in any way.

The purpose of this project is to connect it to my linux box serial port and use the random output as seed for the internal random generator.
 

hippy

Ex-Staff (retired)
Should work, but why not take a single real world, randomly changing bit input ( or more in succession ) and do all the XOR and other processing in software ? You don't need the complexity of the hardware loopback of PICAXE outputs to PICAXE inputs via XOR gates.

To generate a 'truly random' number you'll need to define exactly what that means, and then make sure that your software ( or hardware ) processing delivers that.

A truly random sequence could generate multiple sequences of the same number, or a repeating pattern of numbers ( just like rolling a dice can ); is that suitable for your seeding ? You might want to ensure that the same number is never used consecutively, not again until all other possible numbers have also been used, or must never appear more than 10% of the time.

The concept of 'randomness' is quite complex.
 

boriz

Senior Member
First check here: http://en.wikipedia.org/wiki/Hardware_random_number_generator

If you don’t mind using external discreet components. A simple noise generating circuit could do the job.

Something like this: http://www.uoguelph.ca/~antoon/tutorial/xtor/xtor6/6fig7.gif

Taken from this webpage: http://www.uoguelph.ca/~antoon/tutorial/xtor/xtor6/xtor6.html

Once you have the noise, then you can either low-pass filter it and read random ADC values, or you could just read it as a digital input that will be randomly 0 or 1 and read it 8 times to get a random byte.
 

lazarus

New Member
thanks, I need the random to work in a way that a the next value (byte, word) can not be calculated in any way.
If I only output 1's and 0's for 1.000.000 times I like to see 500.000 of both.

That noise generator seems realy cool and might just do the trick. (probably better than the oscillators)

anyway, to get an even spread it helps to put the output throu a hash generator like MD5 or SHA1. (not that the picaxe will be able to do that but that's not a problem in my case)

I'll let you know what the results of my experiments are.
 

Tom2000

Senior Member
If the built-in 16-bit random function doesn't do the job for you, here's a MPASM routine I use whenever I need a random number. I've never checked my routine in any rigorous fashion, but it seems to return a good distribution and doesn't pattern-lock.

I've done a quick translation into Picaxe BASIC, but haven't tried it. Check if for bugs and typos if you use it. I've aligned the comments in the Picaxe version with those in the MPASM version, which I know works, so you can follow the translation easily.

Good luck!

Tom


<code><pre><font size=2 face='Courier'>




; sub Random
;
; This simulates a 32 stage linear feedback shift register
; (numbered 0 to 31) with taps at stages 17 and 30.
;
; Returns with 32-bit random number in sreg's
;
; Uses stored values in sreg0 through sreg3, temp0, temp1
; Modifies W, temp0, temp1, and sreg0 through sreg3
;
Random
clrf temp0
clrf temp1

; Capture the tap states

btfsc sreg2,1 ; tap 17
bsf temp0,0
btfsc sreg3,6 ; tap 30
bsf temp1,0

; XOR the tap states into the carry bit

movf temp0,W ; xor taps 17 &amp; 30 to temp1
xorwf temp1,W
bcf STATUS,C
btfsc STATUS,Z
bsf STATUS,C

; Roll the carry into the shift register chain

rlf sreg0,F
rlf sreg1,F
rlf sreg2,F
rlf sreg3,F

return


;--------------------------------------------
;
; Picaxe translation of the MPASM routine
;
;--------------------------------------------

symbol Seed = w6 ; b12 and b13
symbol sreg0 = b0
symbol sreg1 = b1
symbol sreg2 = b2
symbol sreg3 = b3
symbol temp0 = b4
symbol temp1 = b5
symbol temp2 = b6
symbol temp3 = b7

Main:

; Seed routine:

; Count rapidly and randomly until the user does something

do
inc Seed
loop until &lt;user does something - presses a button, adjusts a pot, etc.&gt;

sreg0 = b13 ; load seed into shift registers
sreg2 = b12
sreg1 = b12 ^ b13
sreg3 = b12 + b13

; Call the random number generator

gosub PRGen

; do something with sreg0 - sreg3 here

do
loop

end


PRgen:

; capture the tap states

temp2 = sreg2 &amp; %00000010 ; bit 17
if temp2 &gt; 0 then
temp0 = 1
else
temp0 = 0
endif

temp2 = sreg3 &amp; %01000000 ; bit 30
if temp2 &gt; 0 then
temp1 = 1
else
temp1 = 0
endif

; XOR the tap states into the carry bit

temp2 = temp0 ^ temp1

; Roll the carry into the shift register chain

if sreg0 &gt; 127 then ; capture sreg0's carry bit to temp0
temp0 = 1
else
temp0 = 0
endif

sreg0 = sreg0 * 2 + temp2 ; rlf sreg0, roll xor'd bit 17 and bit 30 into sreg0's LSB

if sreg1 &gt; 127 then ; capture sreg1's carry bit to temp1
temp1 = 1
else
temp1 = 0
endif

sreg1 = sreg1 * 2 + temp0 ; rlf sreg1, roll sreg0's carry bit into sreg1's LSB

if sreg2 &gt; 127 then ; capture sreg2's carry bit to temp0
temp0 = 1
else
temp0 = 0
endif

sreg2 = sreg2 * 2 + temp1 ; rlf sreg2, roll sreg1's carry bit into sreg2's LSB

sreg3 = sreg3 * 2 + temp0 ; rlf sreg3, roll sreg2's carry bit into sreg3's LSB


return ; with a random number in sreg3 (MSB) through sreg0 (LSB)



</font></pre></code>











Edited by - Tom2000 on 27/06/2007 15:17:31

Edited by - Tom2000 on 27/06/2007 16:05:57
 
Top