PDA

View Full Version : Ramdom numbers



Brian Carpenter
08-24-2005, 12:49 PM
in order to test my project i need to have my stamp generate a number randomly between 1 and 4 every 3 seconds or so. Doesnt have to be exact. i am just having issues with the random part. how would i write this code.

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


It's Only A Stupid Question If You Have Not Googled It First!!

Jon Williams
08-24-2005, 08:18 PM
The RANDOM function is pseudo-random (mathematically generated), so to get true randomness you need to use some human intervention (button press).ˇThe code below shows how to tumble the RANDOM function until a button is pressed, then scale it as youˇdesire.

Main:
ˇ DO
ˇˇˇ RANDOM randVal
ˇ LOOP UNTIL (Select = Pressed)

ˇ selection = randVal // 4 + 1

ˇ ' rest of your code here

ˇ GOTO Main

'Select' is the button definition (PIN) and 'Pressed' is the button state when pressed (1 or 0).

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Brian Carpenter
08-24-2005, 11:13 PM
thank you Jon for helping me with my ,what some would thing, ignorante questions

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


It's Only A Stupid Question If You Have Not Googled It First!!

Jon Williams
08-24-2005, 11:26 PM
Ignorance is simply not knowing -- no crime in that. What would be criminal is not to apply what you now know and make it your own so you can pass that along some day.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Ryan Clarke
08-24-2005, 11:56 PM
The 'seed' is how you get 'better' randomness in code- as Jon said, a human pressing a button adds a seed (because the button will not be pressed at exactly the same time as previously pressed-

I know some people that seed off of variations of the system clock (on computer code)- this still isn't *true* randomness, but since it is then dependent on when you start the program and variations from the system clock it can appear very random indeed.

Ryan

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Ryan Clarke

Parallax Tech Support
rclarke@parallax.com (mailto:rclarke@parallax.com)

Brian Carpenter
08-25-2005, 12:43 AM
thanks guys

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


It's Only A Stupid Question If You Have Not Googled It First!!