Shop OBEX P1 Docs P2 Docs Learn Events
random number — Parallax Forums

random number

Harry1Harry1 Posts: 29
edited 2007-03-29 20:01 in Propeller 1
how do i generate a random number from 0 to 22
thanks!

Comments

  • CJCJ Posts: 470
    edited 2007-03-27 19:43
    random := myvar? // 23

    myvar is your variable that holds the random number
    random is the resulting random number from 0 to 22

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.
  • T ChapT Chap Posts: 4,223
    edited 2007-03-27 20:03
    If it is called at the same point after boot everytime, it may not be so random. It should be triggered by some event, or use the Random Number Generator Object that Chip recently posted, which likely needs modifying with limits in your case.

    Post Edited (TChapman) : 3/27/2007 8:10:07 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-27 20:07
    If you have any loops in your program where you wait for an external event whether it's a keypress or button press or a serial input character, put "myvar?" in the loop. This will "stir" the random sequence in a way that's dependent on the "hard to predict" external event.
  • cgraceycgracey Posts: 14,133
    edited 2007-03-27 21:01
    Here's a link to·a real random number generator I made last week:

    http://forums.parallax.com/showthread.php?p=639978

    It takes 32 longs of code space and 2 longs for variables. It takes one cog, but you only need to run it when you need a new random number:

    OBJ

    · rr : "RealRandom"

    PRI random_0_to_22 : value

    · rr.start
    · value := ||rr.random // 23
    · rr.stop

    This will never yield a repeating pattern.

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


    Chip Gracey
    Parallax, Inc.

    Post Edited (Chip Gracey (Parallax)) : 3/27/2007 9:05:57 PM GMT
  • Harry1Harry1 Posts: 29
    edited 2007-03-29 20:01
    Thank you guys for your help!! XD
Sign In or Register to comment.