Shop OBEX P1 Docs P2 Docs Learn Events
Can someone explain what "random" does? — Parallax Forums

Can someone explain what "random" does?

lardomlardom Posts: 1,659
edited 2011-01-16 19:59 in General Discussion
There is an operator called Random Number Forward "?X" and Reverse "X?" which generate integers that are a puzzle to me. I modified an object I found in the OBEX to display those random numbers on the PST. I've attached a screenshot of the PST display and the modified object itself. The random numbers are being used to control pulse width which works but which makes no sense to me.:nerd:

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-15 10:06
    The RANDOM functions in the Propeller and BASIC Stamp use what's called an LFSR (linear feedback shift register) -- an algorithm -- to create a pseudo-random value. It's called pseudo-random because, while it looks random, it really isn't. The same seed will give the same output.

    In the BASIC Stamp RANDOM only goes forward, in the Propller, ? can go either direction. Here's a simple Propeller demo that demonstates that ? is math-based, not truly random:
    seed := 1234
    
      repeat 5
        term.dec(seed)
        term.tx(GOTOX)
        term.tx(20)
        ?seed
        term.dec(seed)
        term.tx(CR)
    
      term.tx(CR)
    
      repeat 5
        term.dec(seed)
        term.tx(GOTOX)
        term.tx(20)
        seed?
        term.dec(seed)
        term.tx(CR)
    

    If you look at the screen-shot of the output you'll see that the starting seed (1234) is returned to after running ? backward the same number of times we went forward.

    A lot of my coding for props (movie and Halloween props, that is) and what I tend to do is use some external event to drive the randomness. For example, I might do something like this in a Halloween prop:
    repeat
        ?seed
      until (checktrigger == true)
    
      delay := ||seed // 4001 + 1000
      pause(delay)
    

    This code will stir the random number (seed) until a valid trigger event, then create a delay from that. By using an external event the number becomes truly random. You can't always do this, and there is an object written by Chip Gracey that gives very random numbers without an external event; that said, it does require an extra cog.
    443 x 444 - 175K
  • lardomlardom Posts: 1,659
    edited 2011-01-15 21:23
    JonnyMac, thanks for the insight. I'll study Pbasic documentation on the operator also. LFSR is another new concept.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2011-01-16 00:25
    I realize some computer topics Never Ever Change and random number generators is one of those. The Gospel is that it is Pseudo-random as a seed number and a method is used.

    But in recently reading about electronic noise in op amps, I found the comment that thermal noise in a resistor is random (truely random Brownian motion). So why can't one merely amplify the noise, sample its level as required as a byte or word or whatever, and use that as your random number?
  • LeonLeon Posts: 7,620
    edited 2011-01-16 02:52
    That is sometimes done. The noise depends on external factors, so it isn't an ideal technique. The best RNGs use quantum techniques.
  • max72max72 Posts: 1,155
    edited 2011-01-16 03:59
    in Monte Carlo methods the repeatability of a sequence starting from the same seed is a key point, mainly during software validation, so in many cases "true" random is not better than pseudo random..

    http://en.wikipedia.org/wiki/Monte_Carlo_method

    Massimo
  • Heater.Heater. Posts: 21,230
    edited 2011-01-16 07:52
    Loopy Byteloose,

    I always liked the two transistor avalanche noise generator as shown nicely here: http://www.cryogenius.com/hardware/rng/

    It appeals to me because of the odd way the transistors are wired up.

    Don't forget to apply the Von Neumann method in software to un-bias the results (ensure there are equal numbers of zeros and ones).

    Haven't made one for the Prop yet but often thought I want to.

    Never mind about interference from outside and superiority quantum noise sources. I'm sure for 99% of normal uses this is as good as it gets. Run it from batteries in a lead box with optical coupled output. The noise from all the consciousness' around will upset it anyway:)
  • lardomlardom Posts: 1,659
    edited 2011-01-16 08:39
    I see several new concepts. It's starting to look like a textbook chapter. "What could I use this for?", sounds like a grade-school question. There is a lot of ground to cover in "?X" and "X?". I now want to learn more.
  • tonyp12tonyp12 Posts: 1,951
    edited 2011-01-16 09:09
    For most people,
    if you base the SEED on a free running counter that stops when a user presses a button (and maybe also release) should be considered good enough.

    Phil created a randomizer based on PLL jitter.

    Running four of the props counter at different speeds, you should be able to create something below.
    Quote:
    All VIA C3 microprocessors have included a hardware RNG on the processor chip since 2003.
    Instead of using thermal noise, raw bits are generated by using four freerunning oscillators which
    are designed to run at different rates.
    The output of two are XORed to control the bias on a third oscillator, whose output clocks the output
    of the fourth oscillator to produce the raw bit. Minor variations in temperature, silicon characteristics,
    and local electrical conditions cause continuing oscillator speed variations and thus produce the entropy of the raw bits.

    http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/evaluation_summary_padlock_rng.pdf
  • ercoerco Posts: 20,260
    edited 2011-01-16 18:11
    BASIC Stamp or not, this video clearly shows the transition from order to "Random": http://www.youtube.com/watch?v=stDWNam7RtE
  • TtailspinTtailspin Posts: 1,326
    edited 2011-01-16 19:59
    Life is Harsh...
Sign In or Register to comment.