Shop OBEX P1 Docs P2 Docs Learn Events
Controlled Random Values — Parallax Forums

Controlled Random Values

GortGort Posts: 4
edited 2009-05-20 00:31 in Propeller 1
A fellow student and I were given an assignment to make a reaction timer with a Propeller chip. Essentially, we need to have a random amount of time pass before the user reacts to a change (possibly LEDs, possible something with a monitor), within limits. The only random function we have been able to find is ?, which gives values far too long to be of any use. Is it possible to generate random values between 1_000_000 and 100_000_000, for example?

Thanks

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2009-05-19 04:51
    So what you are saying is that if your randomValue > 100_000_000 then you want it to be smaller but if randomValue < 1_000_000 you want it to be bigger else it is ok?

    Or maybe you you could limit the maximum and limit the minimum value returned somehow.

    Rich H
  • SRLMSRLM Posts: 5,045
    edited 2009-05-19 05:22
    Must not be a programmer... Use the modulus operator.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-05-19 13:15
    ? // 99_000_000 + 1_000_000
  • KyeKye Posts: 2,200
    edited 2009-05-19 13:24
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • JonnyMacJonnyMac Posts: 9,195
    edited 2009-05-19 15:33
    Actually, the remainder will be between 0 and the divisor minus one, so you may need to rewrite as:

    ? // 99_000_001 + 1_000_000

    I spend a good deal of time using this kind of code in Halloween props for randomized timing. That said, in this particular application it's meaningless as it's one clock tick for waitcnt. Still, from a generalized point of view the formula is:

    random_value // (max - min + 1) + min
  • GortGort Posts: 4
    edited 2009-05-19 20:35
    We tried using that code like this:

    x := ? // 99_000_001 + 1_000_000

    dira[noparse][[/noparse]Pin]~~
    repeat 10
    !outa[noparse][[/noparse]Pin]
    waitcnt(x + cnt)


    But the program continually says it needs a variable and highlights the modulus.

    We have no idea what we are doing and could use more in depth help if possible.
  • rokickirokicki Posts: 1,000
    edited 2009-05-19 20:51
    Please post your full code.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-05-19 21:00
    x:= ?x//99_000_001+1_000_000

    Forgot that ? is always an assignment, so it needs a variable it can assign the value to.


    PS:
    Maybe it's better to use a different variable, as x is used as seed for the next random value. So, due to the modulus it might be possible that there exists a value which would lead to problems. Either a shorter random loop or it get's locked to one value.

    So, better to use:
    var
    long x, rnd
    ...
    x := ?rnd // 99_000_001 + 1_000_000

    Post Edited (MagIO2) : 5/19/2009 9:11:47 PM GMT
  • GortGort Posts: 4
    edited 2009-05-19 22:29
    var
    long x, rnd
    
    
    
    PUB Main
      DIRA[noparse][[/noparse]0..2] := %110               'Sets pins 0 and 1 as output, pin 2 as input
    
      
    repeat
      waitpeq(%10, %10, 0)             'Waits until Pin 1 is high
        outa[noparse][[/noparse]0] := 1                   'Sets Pin 0 high
      waitpne(%10, %10, 0)             'Waits until Pin 1 is low
        outa[noparse][[/noparse]0] := 0                   'Sets Pin 0 low
        Toggle(5)
    
    PUB Toggle(Pin)
    
    x := ?rnd // 9_000_001 + 1_000_000
    
    dira[noparse][[/noparse]Pin]~~
      repeat 10
        !outa[noparse][[/noparse]Pin]
        waitcnt(x + cnt)    
    
    




    The code compiles fine now, but the LED on pin 5 stays on forever without flashing.
  • KyeKye Posts: 2,200
    edited 2009-05-20 00:14
    Well, It may be flashing to fast for you to see it.

    Since the system counter is incremented 80_000_000 times per second maybe you want some kind of constant added to the waitcnt part. Like, try adding 70_000_000 to the waitcnt part.

    This will make the processor stop long enough for you to see it flashing hopefully.

    Maybe you might want a different type of indicator since LEDs need to flash slowly for people to see what's happening in comparison to the speed of the processor.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • GortGort Posts: 4
    edited 2009-05-20 00:31
    I was able to get it to a state we can use:

    PUB Toggle(Pin)
    
    x := ?rnd // 2_500_001 + 1_000_000
    
    dira[noparse][[/noparse]Pin]~~
      repeat 10
        !outa[noparse][[/noparse]Pin]
        waitcnt(x + 1_000_000 + cnt)
    



    The flashes will sometimes be quite long, and will sometimes be quite short.

    Thanks!
Sign In or Register to comment.