Shop OBEX P1 Docs P2 Docs Learn Events
True Random Number — Parallax Forums

True Random Number

T ChapT Chap Posts: 4,223
edited 2007-03-17 06:23 in Propeller 1
Anyone have an idea on generating a True random number?

?X is not it

Incorporating the CNT into a formula may work, but even then it is derived. I am looking for the closest thing to the brain picking a number between -2B and 2B, preferably not time based.

I suppose a computer can do nothing without some formula even in generating randomness.

Comments

  • T ChapT Chap Posts: 4,223
    edited 2007-03-17 02:56
    This is the closest I can find.


    
       Num := CNT
       Num := ?Num
    
    
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-03-17 02:57
    The only means for generating a true random number is by measuring a random system, it is simple not possible to compute a random number, hence all computationally generated numbers are called psuedo-random.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • CJCJ Posts: 470
    edited 2007-03-17 02:58
    why not just let a couple of pins float and add them to an accumulator for a little while, to create your seed

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.
  • T ChapT Chap Posts: 4,223
    edited 2007-03-17 03:04
    CJ, that is the idea, something by which some external, unknown source can affect it.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-03-17 03:11
    Unfortunately a pin floating isn't a truely random process (its a biased system) and would require a mathmatical transform to unbias it. If you are looking for a random seed generator, the easiest is a pushbutton recording the cnt. If you want a hands off method there is a means of taking a noisy process (such as a zener diode) and amplifying it into a stream of binary noise. But since it seems what you are really asking for a relatively random seed and not a true random number generator, a circuit such as http://www.discovercircuits.com/DJ-Circuits/noisegen.htm·would likely work just fine.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 3/17/2007 3:23:05 AM GMT
  • cgraceycgracey Posts: 14,133
    edited 2007-03-17 03:28
    Hah! How about this:

    The internal ~20KHz oscillator is a free-running analog oscillator and is used to sync between clock selections when CLKSEL is written to (via CLKSET(mode, frequency). It takes two of these clock cycles to get through a CLKSET instruction in Spin, but how long this actually takes varies, because of the phase relationship to the ~20KHz clock (~50us to ~100us of delay will occur). So, if you do a:

    t := cnt

    clkset(clkmode,clkfreq)·· 'rewrite CLKSEL with the same data

    t := cnt - t

    ...While running at 80MHz, there will be a random ~50us variance in t which, at 80MHz, will translate into ~4000 units. The only problem with this is that the whole chip's master clock will slow to ~20KHz for one to two cycles, so a big time hiccup occurs.

    I think that practically speaking, if you are waiting on some asynchronous external event to begin some process that needs a random number, the CNT will be effectively random, especially if the random ? operator is used.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
  • CJCJ Posts: 470
    edited 2007-03-17 03:40
    I thought CNT was clocked by whatever the chip is running at, so how would that get you a random number? unless CNT is remaining clocked by the 80Mhz during the 20Khz steps

    please correct me if I am off

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.
  • T ChapT Chap Posts: 4,223
    edited 2007-03-17 03:45
    Thanks for the ideas. There is a start event of the cycle, by any means really, including F10 or buttons. I have some real goals in mind for the random number, but, it's the weekend and fun stuff takes over now!

    I want to be able to affect the outcome of the number with thoughts. If the number is above 0, a Green LED and a tone of 1k sounds on a speaker. If < 0, a Red LED and a of tone of 700 plays.

    I was studying the supposedly "verifyable" effects of thought on the outcome of physical events from people concentrating on the test. If a ball can be affected as to where is lands after striking a number of randomly placed obstacles in it's downward path, then one of two choices of LEDs should be able to get affected as well. We'll soon see if this is true. I am testing Chips code now.

    Post Edited (originator) : 3/17/2007 3:51:39 AM GMT
  • CJCJ Posts: 470
    edited 2007-03-17 03:51
    chip, I think I see what it gets you now, it is still ticking along at 80M until the 20k is ready, hops over for a couple, then hops back, leaving an indeterminate amount of ticks on CNT

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-03-17 05:15
    Here's something that appears to work. I connected a 100K resistor from A7 to A6, and another 100K resistor from A6 to ground. Then I programmed A7 to be the output pin for an A/D converter and A6 to be the feedback pin. 'No caps, just resistors. In this condition, A7 outputs at about about an 80% duty cycle, with random phase shifts. By reading A6 and shifting it into a 32-bit variable, then applying the ? operator a number of times, I'm getting what look to be random numbers. Of course, it will take further analysis to determine just how random they are.

    Here's the program listing:

    [b]CON[/b]
    
      [b]_clkmode[/b]      = [b]xtal[/b]1 + [b]pll[/b]16x
      [b]_xinfreq[/b]      = 5_000_000
    
      out           = 7
      inp           = 6 
    
    [b]OBJ[/b]
    
      pr : "tv_wtext"
           
    [b]PUB[/b] start
    
      pr.start(12)
      [b]dira[/b] := 1 << out
      [b]ctra[/b] := constant(%01001_000 << 23 | out << 9 | inp) 
      [b]repeat[/b] 32
        pr.hex(random, 8)
        pr.[b]str[/b]([b]string[/b](" "))
      [b]repeat[/b]
    
    [b]PUB[/b] random | v
    
      [s]v := $12345678[/s] [i]Not needed. It was part of a different experiment.[/i]
      [b]repeat[/b] 32
        v := v << 1 | [b]ina[/b][noparse][[/noparse]inp]
      [b]repeat[/b] 32
        ?v
      [b]return[/b] v
    
    
    


    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 3/17/2007 5:39:05 AM GMT
  • T ChapT Chap Posts: 4,223
    edited 2007-03-17 05:31
    Interesting idea, I'll plug it in as well, thanks Phil
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-03-17 05:44
    For your range of numbers, you can change the print statement to:

        pr.dec(random >> 1 // $56 - $2B)
    
    
    



    -Phil
  • cgraceycgracey Posts: 14,133
    edited 2007-03-17 06:23
    You got it. It keeps running at 80MHz until the unrelated 20KHz edge occurs.
    CJ said...
    chip, I think I see what it gets you now, it is still ticking along at 80M until the 20k is ready, hops over for a couple, then hops back, leaving an indeterminate amount of ticks on CNT

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
Sign In or Register to comment.