Need a source of random numbers for Spin programs!
Dennis Ferron
Posts: 480
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.
Has anyone got a good random number object for the Propeller? If not, I'll write one and post it.
Comments
last = cnt & 4095
prime = large 16 bit prime
another = another large 16 bit prime
...
prnd = (last * prime + another) & 65535
last=prnd
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
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
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