Controlled Random Values
Gort
Posts: 4
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
Thanks
Comments
Or maybe you you could limit the maximum and limit the minimum value returned somehow.
Rich H
Nyamekye,
? // 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
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.
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
The code compiles fine now, but the LED on pin 5 stays on forever without flashing.
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,
The flashes will sometimes be quite long, and will sometimes be quite short.
Thanks!