Page History: True Random Generator (companion code for circuit)
Compare Page Revisions
Page Revision: 2017/03/09 08:32
This function will generate a true random number from the
Random Bit Generator for cryptographic strength random numbers circuit.
An upper limit and resolution must be specified. The higher the resolution the longer time required to return a value but the less "course" the answer. e.g. if you specify a maximum of 100 with a resolution of 4 bits (2^4=16), the smallest value that can be returned is 6.25 (100/16), whereas a resolution of 16 (2^16=65536) will return a minimum resolution of 0.0015259 (100/65536). Choose a resolution which is a good compromise of resolution versus speed.
Syntax:
TRnd(Upper_Limit,Resolution)
Resolution is
1 < x < 64
bits
Return is
1 <= x < Upper_Limit
Example:
t!=TRnd!(100,16)
Assumptions:
SETPIN 1,DIN
Code:
FUNCTION TRnd!(x AS FLOAT,bits AS INTEGER)
LOCAL INTEGER n,t,b
b=bits
IF b>63 THEN b=63
IF b<1 THEN b=1
FOR n=1 TO b
t=t*2 + PIN(1)
NEXT
TRnd!=(x /(2^b)) *t ' the returned value can never be x but might be very close
END FUNCTION