random number for 14M

rolsen

Member
How do I generate a random number between 1 and 15 inclusive and use number generated to point to a command i.e.

generate random number between 1 & 15 incl
if number generated = x then do_somthing
if number generated = y then do_something_else

I am using 14M picaxe

I have tried to use manual instruction but having trouble getting it right.
 

westaust55

Moderator
There are several ways to get a pseudo-random number.
1. use the RANDOM number (needs a word variable)

or other ways do you have
2. a clock? (can use seconds as basis)
3. a button that can be pressed for a period of time? (use pulsin as basis)

let us know a bit more about what hardware you have and a more detailed example can be given.

Once you have a "random" number in say word variable w1,
a program line like:

b5 = w1 AND 15

This gives a value from 0 to 15 in b5
If you have the value 0 then just run the random number generator again until it is non-zero.

end result is a number from 1 to 15


For the second question, you can use the ON.... GOTO or the ON...GOSUB commands to branch. Have a look at PICAXE manual 2 pages 102 and 103.
 
Last edited:

moxhamj

New Member
I haven't got the syntax in front of me but in pseudo code;
peek 80,word w0' store in ram otherwise reseeds to the same number
random w0
poke 80,word w0' store the new number so reseeds from this new number
w1=w0/4096' get a number 0-15
then I think there is a command; on xx gosub a,b,c,d
but if not, use select case w1
 

Dave-nz2

New Member
Range selection of random number

Code:
  random w0
  w0 = w0 // 15 + 1     'reduces w0 to the range 1 .. 15 incl.
The "w0 // 15" provides the range {0..14},
and then "+ 1" shifts the resultant range to {1..15}
 
Last edited:

rolsen

Member
Random Number 14M

Thanks Dave and everyone

Just what I needed - The dice concept was a good reference to what I needed (A dice with fifteen numbers instead of six). Code works well now. I'm using it in a board game.

much appreciated
 
Top