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

random number

Jonathan HolleJonathan Holle Posts: 48
edited 2010-08-22 08:33 in Propeller 1
Hi all,

Do you know how I could generate a random number from 1 to 6 as a dice ?

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-08-22 01:30
    dice_roll := ||(? seed) // 6 + 1
    

    where seed is a long. If that doesn't make sense, look up the ? operator in the Spin manual.

    -Phil
  • Jonathan HolleJonathan Holle Posts: 48
    edited 2010-08-22 02:48
    Thanks for your answer. This don't works correctly for my application. Because it need a reset in order to get a new result, I would like to get a new random number everytime the function is called ... :-/
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2010-08-22 06:48
    You need a re-seeding source (a photocell or...) or the die will, naturally, turn up the same result every time.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-08-22 08:33
    If you need a different result after every reset, you could also try Chip's Real Random object in the OBEX:

    http://obex.parallax.com/objects/62/

    -Phil
Sign In or Register to comment.