Shop OBEX P1 Docs P2 Docs Learn Events
Need a source of random numbers for Spin programs! — Parallax Forums

Need a source of random numbers for Spin programs!

Dennis FerronDennis Ferron Posts: 480
edited 2007-06-11 19:55 in Propeller 1
I'm working on a Nibbles game for the Propeller and I can't seem to get any randomness to my computer-controlled snakes. Basically the "artificial stupidity" algorithm that controls the computer player is, when confronted by a situation where a collision will happen soon, try random turns until you escape or you die. But I can't find any random number function in the PropTool library so my snakes end up either in infinite loops because they never take a wrong turn or die because they can't think of a new set of turns.

Has anyone got a good random number object for the Propeller? If not, I'll write one and post it.

Comments

  • Bill HenningBill Henning Posts: 6,445
    edited 2006-11-29 07:11
    Try a variation of an old standby:

    last = cnt & 4095
    prime = large 16 bit prime
    another = another large 16 bit prime
    ...


    prnd = (last * prime + another) & 65535
    last=prnd
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2006-11-29 07:26
    You could use Spin's random operator, "?".
  • potatoheadpotatohead Posts: 10,255
    edited 2006-11-29 07:37
    Grab a seed from the system counter, when the user hits start. That way, they pick their own seed and you no longer worry about it. For a given seed, the spin operator will output the same set of random numbers. This can be very cool for game stuff, if you need repeatable randomness...

    Assign it to a variable, called seed, rand, etc...

    Use this seed in your AS [noparse]:)[/noparse] code

    some_game_variable := seed?

    Grab another seed, upon start, or on other user input, depending on your needs.

    The string of random numbers generated is repeatable backward and forward.

    If you say:

    var := seed?, then you get one set of numbers.

    If you say:

    var := ?seed, then you get another set of numbers.

    The difference is basically when the variable seed, gets put into the random operator. Pre or post.

    Post Edited (potatohead) : 11/29/2006 7:42:53 AM GMT
  • Dennis FerronDennis Ferron Posts: 480
    edited 2006-11-29 19:32
    A random operator. I should have RTFM!
  • Chris MerckChris Merck Posts: 55
    edited 2007-06-09 19:40
    Even after I knew about the "?" operator I had a little trouble with it. I did finally get it to work for my rogue game, here was the result:

    VAR
    long byte

    PUB random_byte : retval
    retval := ||(dice?//256)

    PUB roll(number_of_dice, number_of_sides) : retval
    retval := 0
    repeat number_of_dice
    retval += ||(dice?//number_of_sides) + 1
  • Chris MerckChris Merck Posts: 55
    edited 2007-06-09 19:41
    of course the line after repeat is indented!
  • simonlsimonl Posts: 866
    edited 2007-06-11 19:55
    Hi Dennis,

    Chip Gracey came up with a real random number generator here: http://forums.parallax.com/showthread.php?p=639978

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon
    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif
Sign In or Register to comment.