Shop OBEX P1 Docs P2 Docs Learn Events
Ramdom numbers — Parallax Forums

Ramdom numbers

Brian CarpenterBrian Carpenter Posts: 728
edited 2005-08-24 16:43 in BASIC Stamp
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!!

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-24 12:18
    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 CarpenterBrian Carpenter Posts: 728
    edited 2005-08-24 15:13
    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 WilliamsJon Williams Posts: 6,491
    edited 2005-08-24 15:26
    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 ClarkeRyan Clarke Posts: 738
    edited 2005-08-24 15:56
    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
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2005-08-24 16:43
    thanks guys

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


    It's Only A Stupid Question If You Have Not Googled It First!!
Sign In or Register to comment.