Shop OBEX P1 Docs P2 Docs Learn Events
Stuper Simple Ques - Random #'s — Parallax Forums

Stuper Simple Ques - Random #'s

Agent420Agent420 Posts: 439
edited 2009-09-12 01:44 in Propeller 1
I feel foolish that I can't figure this out after consulting both the manual and a forum search, but...

What is the best method to compute a random number for a desired range.· I'm not concerned about 'true' randomness, I just want to understand the ? operator.· I understand seed values and forward and backward sequences.

The manual states that the ? operator uses a variable's value as the seed and generates and number from NEGX to POSX.·

Say I want a random value from 1 to 10.· In the old Basic days, rnd returned a value from 0->1 that you simply mutliplied by the desired range.· Here it seems you would have to bit AND then modulus or something to scale the returned random value.

TIA


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

Comments

  • heaterheater Posts: 3,370
    edited 2009-09-11 14:05
    Just use the modulus operator on your random number

    result = ?result // 10

    Gives values 0 to 9 so just add 1.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • Agent420Agent420 Posts: 439
    edited 2009-09-11 14:14
    Thank you.· I was totally overthinking this whole thing.· I am spending too much (or not enough) time calculating clock cycles and bit pattern masks to see the tree in the forest ;-)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • AribaAriba Posts: 2,690
    edited 2009-09-11 22:49
    But notice that the random number can be negativ and then also the modulo result is negativ, so you get a number in the range: -9..+9 with // 10 !

    I had to learn this the hard way, with a program that generated 1 random number for every person on an art exibition. Because the first 5 numbers were all positiv, all my test runned OK, but then at the exibition the desaster started after a few persons.... (I got negative indexes into an array).

    Andy
  • heaterheater Posts: 3,370
    edited 2009-09-11 23:01
    Good point. Ouch!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • Roger LeeRoger Lee Posts: 339
    edited 2009-09-12 00:31
    wish I had a dime for every time I've "learned" that modulus trick.

    I ask the same question a month or so ago.

    All I needed was a bump in the right direction.

    Roger
  • localrogerlocalroger Posts: 3,452
    edited 2009-09-12 01:34
    You could always mask the random number with something that eliminates the sign bit, THEN take the modulus for the range you want, e.g. result := (?seed & $7FFF_FFFF) // 10

    You could also shift it right one bit, e.g. result := (?seed >> 1) // 10 which might take less token space since you don't need the 32 bit constant.

    Or, probably even shorter, you could just take its absolute value before using it: result := (||(?seed))//10 Some of those parentheses might not be necessary, I'm doing this off the top of my head.
  • NetHogNetHog Posts: 104
    edited 2009-09-12 01:44
    Modulus gives a badly distributed random number unless its a power of 2 (but is often 'good enough')

    On the subject of Random, I wrote this blog entry a year ago:

    http://it.toolbox.com/blogs/codesharp/more-random-than-random-25106

    Edit to add:

    Fun exercise with random numbers

    http://stackoverflow.com/questions/804316/how-do-i-implement-rand7-in-terms-of-rand5


    Post Edited (NetHog) : 9/12/2009 2:18:07 AM GMT
Sign In or Register to comment.