Shop OBEX P1 Docs P2 Docs Learn Events
Real Random Numbers with SPIN only (one COG spared again!) — Parallax Forums

Real Random Numbers with SPIN only (one COG spared again!)

cessnapilotcessnapilot Posts: 182
edited 2009-09-04 22:54 in Propeller 1
Hi,

During testing of the new version of SPIN_TrigPack object, I serendipitously discovered that after each reboot or after each power off/ on cycle, I got different values from it's Q_Rnd generator. The code of the generator looked like

PUB Init
'-------------------------------------------------------------------------
'----------------------------------┌──────┐-------------------------------
'----------------------------------│ Init │-------------------------------
'----------------------------------└──────┘-------------------------------
'-------------------------------------------------------------------------
''     Action: - Initializes error handling
''             - Initializes Seed of random number generator
'' Parameters: None                              
''     Result: None              
''+Reads/Uses: _CONTINUE, _SEED                   
''    +Writes: e_action, qSeed globals                      
''      Calls: None
''       Note: It is up to the user to define actions on errors or change
''             _SEED in the CON section
'-------------------------------------------------------------------------
e_action := _CONTINUE
qSeed := _SEED                            'CON _SEED = 2009 
REPEAT 2009
  qSeed := ?qSeed
'-------------------------------------------------------------------------
 
 
 
PUB Q_Rnd(qMin, qMax) | r, rn
'-------------------------------------------------------------------------
'----------------------------------┌───────┐------------------------------
'----------------------------------│ Q_Rnd │------------------------------
'----------------------------------└───────┘------------------------------
'-------------------------------------------------------------------------
''     Action: Calculates a random qValue between  Max. and Min. limits  
'' Parameters: - Minimum in Qs15_16 format
''             - Maximum in Qs15_16 format
''     Result: Pseudo-random qValue from the intervall [noparse][[/noparse]Min, Max]. The
''             consecutive results of this procedure are uniformly
''             distributed over the [noparse][[/noparse]Min, Max] intervall.        
''+Reads/Uses: _RND, _INVALID_ARG     
''    +Writes: e_orig, e_kind, qSeed                         
''      Calls: SPIN_TrigPack_Error
''       Note: Seed is a global variable in this object
'-------------------------------------------------------------------------
IF (qMin == qMax)
  e_orig := _RND
  e_kind := _INVALID_ARG
  SPIN_TrigPack_Error      'This will decide what to do: CONT/NOTIFY/ABORT
  RETURN
 
IF (qMin > qMax)           'Let us allow for this
  r := qMax
  qMax := qMin
  qMin := r
r := qMax - qMin
  
qSeed := ?qSeed + CNT
 
rn := qSeed >> 16          'Transform into the [noparse][[/noparse]0, 1] Qvalue interval
 
RESULT := qMin + Qmul(r, rn)
'-------------------------------------------------------------------------


And the code used for testing was

PUB DoIt | a, b, c, d, e, x, y
'-------------------------------------------------------------------------
'-----------------------------------┌──────┐------------------------------
'-----------------------------------│ DoIt │------------------------------
'-----------------------------------└──────┘------------------------------
'-------------------------------------------------------------------------
''     Action: -Loads PST driver
''             -Does some test calculations 
'' Parameters: None                                 
''     Result: None                    
''+Reads/Uses: PST CONs               
''    +Writes: None                                    
''      Calls: Parallax Serial Terminal---------->PST.Start
''                                                PST.Char
''                                                PST.Chars
''                                                PST.Str
''                                                PST.Stop
''             SPIN_TrigPack--------------------->Q.Init
''                                                Q.StrToQval
''                                                Q.Q_Rnd
''                                                Q.QvalToStr
'-------------------------------------------------------------------------
'Start Parallax Serial Terminal. It will launch 1 COG 
PST.Start(57600)
WAITCNT(6 * CLKFREQ + CNT)
PST.Char(PST#CS)
PST.Str(STRING("SPIN TrigPack demo started..."))
PST.Chars(PST#NL, 2)
Q.Init 
WAITCNT(CLKFREQ + CNT)
 
x := Q.StrToQval(STRING("-1.0"))
y := Q.StrToQval(STRING("1.0"))

 
REPEAT 10
  PST.Str(STRING("RND = "))
  a := Q.Q_Rnd(x, y)
  PST.Str(Q.QvalToStr(a))
  PST.Char(PST#NL)
 
PST.Char(PST#NL)
PST.Str(STRING("SPIN TrigPack demo terminated normaly..."))
WAITCNT(CLKFREQ + CNT)   
PST.Stop
'-------------------------------------------------------------------------


I list here the very first random Qvalues values after 6 power OFF/ONs

·0.3976
-0.9604
-0.3183
·0.256
·0.9972
-0.4189

And the first random Qvalues after 6 hardware reset with continuous power

-0.2107
·0.9571
·0.7968
·0.6058
-0.7099
·0.1471

(Each of these numbers was followed by the next· 9 pseudorandom Qvalues, according to the test code.) The hardware was a Demoboard with an EasyBlueTooth module powered up but just hanging on pins A0, A1 (Rx, Tx) without using those pins by the SPIN_TrigPack_Demo and the SPIN_TrigPack object. First I did not want to disassemble this board since I use it in a parallel project of a piconet with a 6DOF IMU, a 3-axis magnetometer (2 HM55B), a GPS data transmitter (Ant. + Prop. + EasyBT500) and a touch screen color LCD to form an electronic kneepad with distributed "airwired" sensors. Then I took the trouble and removed the EasyBT500 from the board and switched off every other EasyBT500 (and Phone, BT Stick, etc...) around. However I got "Real-Random Like" non repeating values after each program start, again and again... Well, that Prop is certainly "weared and teared"· because of the·serious and numerous·programming errors of mine. So, I wired up a brand new one (DIP40, not for sale!) only to get similar real-random sequences.

The bright side of this that SPIN_TrigPack will arrive (~2 days) with a Real Random generator without extra COG, the dark side is, however, that I do not know why. Maybe someone can figure it out.


Cheers,

Istvan·

Post Edited (cessnapilot) : 9/3/2009 7:32:21 PM GMT

Comments

  • HollyMinkowskiHollyMinkowski Posts: 1,398
    edited 2009-09-03 19:50
    Very interesting...no idea how it might generate real random numbers.
    Maybe Mike Green will puzzle it out.

    Usually real random numbers only come from a quantum random source like
    radioactive decay, brownian motion...etc

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Some mornings I wake up cranky.....but usually I just let him sleep -
  • max72max72 Posts: 1,155
    edited 2009-09-03 20:25
    In the computer world there are not real random but pseudo random.
    en.wikipedia.org/wiki/Pseudorandom_number_generator
    As stated in wikipedia, Montecarlo methods, cryptography and so on rely on pseudo random, and the system works smoothly.
    The advantage with pseudorandom is if you start from the same seed of the random numbers you'll replicate the same sequence.
    On the other hand the cultprit is the correlation of the numbers. For how long the numbers are not correlated, and so can be accepted as random numbers?

    Cessnapilot is pushing the limits of spin only math... really intriguing.. Propeler III with less COGs?

    Massimo
  • HollyMinkowskiHollyMinkowski Posts: 1,398
    edited 2009-09-03 20:36
    If the prop really is spitting out real random numbers then
    each time it generates one number instead of another it
    may be splitting off a parallel universe smile.gif

    1323_253x190.jpg
    www.qubit.org/people/david/Articles/Frontiers.html

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Some mornings I wake up cranky.....but usually I just let him sleep -

    Post Edited (HollyMinkowski) : 9/3/2009 8:41:34 PM GMT
  • NetHogNetHog Posts: 104
    edited 2009-09-03 21:18
    Q_Rnd is using "CNT" in it's algorithm. Not only can this ruin the pseudo-random distribution, it is still considered deterministic unless events that call Q_Rnd themselves are not deterministic.

    Cryptographic random number generators (that are not based of "real" random number generators) obtain as much entropy as possible (e.g. all the hardware + ram contents + how long a computer has been running + what time of day + etc etc) to seed the pseudo random number generator, but then uses the pseudo random number generator from that point onwards.
  • cessnapilotcessnapilot Posts: 182
    edited 2009-09-04 22:54
    Hi All,

    SPIN_TrigPack upgrade has been placed·on OBEX. You can try the first Fixed-point package there or you can try the first True Random Generator with the Propeller microcontroller using only SPIN. Sequences will repeat themselves only after the end of Times. Thanks to all who helped in the development.

    Cheers,

    Istvan
Sign In or Register to comment.