# 08m2,axe133y,random

#### nhphantom

##### New Member
Hi
I am new to Pixaxe I have worked with basic stamp a little . What I am trying to do is use a 08M2 And A Axe133y to make something like a magic 8 ball were the user / person asking the question then push a button this will generate a random number between 1 and lets say 7 it may go up to 16 I don't know yet. I'm thinking something like

code
Random wo
w1 = w0//7+1

select case w1
Case 1
serout c.0,N2400,(254,128)
paues30
serout c.0,N2400,(253,1)
pause 30

what I am having a hard time understanding is how to use a button push to seed the random I have been looking on the forum and found some code that refers to this I just don't understand how or were to use them the codes I found are

code
Do kickrandomnumber
If pinc.o = 1 then
Gosub pushed
Do: loop while pin c.1 = 1
End if
loop

Pushed:
Random w0
w1 = w0//16+1

this is one I found another is
code
Main:
random w0
if pin c.0 = 1 then
let pins b = b1
pause 100
End if
GOTO Main

not sure what will work I hope I make sense

if it' not to much to ask I would like to know what will work and why and maybe how it woks

Thank you
David

#### hippy

##### Senior Member
The basic premise is to keep altering the random number so one doesn't know what it will be when the button is pushed. The basic starting point would be ...
Code:
``````#Picaxe 08M2
#Terminal 4800

Symbol PUSH_BUTTON  = pinC.1
Symbol randomNumber = w7

MainLoop:
Do
Random randomNumber
If PUSH_BUTTON = 1 Then
SerTxd( "Random number is ", #randomNumber, CR, LF )
Do : Loop While PUSH_BUTTON = 1
End If
Loop``````
You may have to adjust that if your push button isn't on pin C.1

That will show a random number in the PE6 terminal display every time you push the button. It will be a number much bigger than you want but it's enough to show that a random number is random, and a different random number will be produced when the button is pushed.

Making it a usable number, and updating the AXE133 can be added later.

#### westaust55

##### Moderator
To make the random numbers a little more random, you could consider to use the TIME function
To seed the RANDOM

take the duration between push button operations or from start to first push button operation. Don’t forget to start the timer function.

#### The bear

##### Senior Member
Hi nhphantom, Can you give me a clue, as to "nh" location.
Bear..

Last edited:

#### The bear

##### Senior Member
Hi nhphantom,
Looks very nice, just south of the Canadian border. There is a member on this forum from the Ossipee area.
Bear..

#### nhphantom

##### New Member
Hi nhphantom,
Looks very nice, just south of the Canadian border. There is a member on this forum from the Ossipee area.
Bear..
Ossippe is about 1 1/2 hours north/north east of me

#### nhphantom

##### New Member
To make the random numbers a little more random, you could consider to use the TIME function
To seed the RANDOM

take the duration between push button operations or from start to first push button operation. Don’t forget to start the timer function.

#### nhphantom

##### New Member
The basic premise is to keep altering the random number so one doesn't know what it will be when the button is pushed. The basic starting point would be ...
Code:
``````#Picaxe 08M2
#Terminal 4800

Symbol PUSH_BUTTON  = pinC.1
Symbol randomNumber = w7

MainLoop:
Do
Random randomNumber
If PUSH_BUTTON = 1 Then
SerTxd( "Random number is ", #randomNumber, CR, LF )
Do : Loop While PUSH_BUTTON = 1
End If
Loop``````
You may have to adjust that if your push button isn't on pin C.1

That will show a random number in the PE6 terminal display every time you push the button. It will be a number much bigger than you want but it's enough to show that a random number is random, and a different random number will be produced when the button is pushed.

Making it a usable number, and updating the AXE133 can be added later.
Hi Hippy
I got a chance to try this code and I believe I understand how it works if I am right it will keep lopping till the button is pressed
then it stores the number in w7 the SerTxd line is not necessary to generate the number using it just to see the numbers. and I see what you mean that the numbers are to big for my needs. so now would do a code to get numbers to were need them as far as updating the AXE133 I think I know how to do it.

Thank you for the help
David

#### hippy

##### Senior Member
Yes, you seem to be grasping everything correctly. The next step is to convert that 'huge number' down to what you need, the 1 to 7 you suggest.

There are a number of ways of doing that, but the one I am favouring today is to add the random value to your 'magicBall' number when the button is pressed. Then making sure it is only between 1 and 7 ...
Rich (BB code):
``````#Picaxe 08M2
#Terminal 4800

Symbol PUSH_BUTTON  = pinC.1
Symbol randomNumber = w7
Symbol magicBall    = w1

MainLoop:
Do
Random randomNumber
If PUSH_BUTTON = 1 Then
magicBall = magicBall + randomNumber
magicBall = magicBall // 7 + 1
SerTxd( "Magic Ball shows ", #magicBall, CR, LF )
Do : Loop While PUSH_BUTTON = 1
End If
Loop``````
The ' magicBall = magicBall // 7' determines the remainder when divided by 7, which will be a number between 0 and 6. Adding a 1 then makes it 1 to 7.

#### nhphantom

##### New Member
Yes, you seem to be grasping everything correctly. The next step is to convert that 'huge number' down to what you need, the 1 to 7 you suggest.

There are a number of ways of doing that, but the one I am favouring today is to add the random value to your 'magicBall' number when the button is pressed. Then making sure it is only between 1 and 7 ...
Rich (BB code):
``````#Picaxe 08M2
#Terminal 4800

Symbol PUSH_BUTTON  = pinC.1
Symbol randomNumber = w7
Symbol magicBall    = w1

MainLoop:
Do
Random randomNumber
If PUSH_BUTTON = 1 Then
magicBall = magicBall + randomNumber
magicBall = magicBall // 7 + 1
SerTxd( "Magic Ball shows ", #magicBall, CR, LF )
Do : Loop While PUSH_BUTTON = 1
End If
Loop``````
The ' magicBall = magicBall // 7' determines the remainder when divided by 7, which will be a number between 0 and 6. Adding a 1 then makes it 1 to 7.
Good afternoon Hippy
I edit the code to your updated one and i am very happy with how it works i even tried changing the 7 in magicBall=magicBall // 7 + 1 and it worked great so that I can change it up or down as needed. I do have a question do the w7 and w1 have a value to them to start with?
Next will the select case w1 work as i think it will or is there a better way ?

After looking at the code some more I believe I will have to exit the loop in some way .

Again thank you for all your help
David

Last edited:

#### westaust55

##### Moderator
RandomNumber (W7) should have a value to “seed” the random number generator.
As per my earlier email if you start and use the TIME command to get a varying number from power up to first push button press then that improves the variability.
As TIME is in seconds and may typically always be low (unless it will be say more than 5 minutes from power up) you could multiply by a fixed number such as 200. If there is any math overflow that is not a problem in this case.

#### hippy

##### Senior Member
do the w7 and w1 have a value to them to start with?
All variables, 'w1' and 'w7' included, are initialised to zero when the PICAXE starts running its program. So there's no need to explicitly set them to any value unless you need them to be non-zero when the program starts.

RandomNumber (W7) should have a value to “seed” the random number generator.
Generally that would be a good idea. But because RANDOM is executed each time round the loop, only grabbed when the button is pushed, it is in effect auto-seeding, will be fairly random the first time the button is pushed so not as essential here.

Having a 'randomNumber = randomNumber + time' might be an idea to stir things up a bit when the button is pushed.

Next will the select case w1 work as i think it will or is there a better way ?
I expect that probably is the best way. Things which would be common to each CASE, such as clearing the screen, positioning the cursor to start of line 1 and so on, can be moved out of the CASE clauses and put before the SELECT CASE itself.

If you are putting out individual messages within each CASE, as you probably will be, an alternative is to put all your messages in Data Eprom and select which is used based on the 'magicBall' value.

There is probably not a lot of benefit in doing that in this case, it would mostly just complicate the code. It might be worth it if you had a lot of messages, but then there's only limited Data Eprom; you could only have 16 messages of 16 characters without making the code even more complicated. One solution to that would be to move to SELECT CASE. Back to what is proposed.

#### nhphantom

##### New Member
Sorry this awhile but life got in the way I tried to get it to give the answers to the axe133 but I get nothing here is want I tried .

code
#Picaxe 08M2
#Terminal 4800
Symbol PUSH_BUTTON = pinC.1
Symbol randomNumber = w7
Symbol magicBall = w1
Symbol baud = N2400
Symbol Oled = c.2
MainLoop:
Do
Random randomNumber
If PUSH_BUTTON = 1 Then
magicBall = magicBall + randomNumber
magicBall = magicBall//4 + 1

SerTxd( "Magic Ball shows" , #magicBall,CR,LF)

Do: Loop while PUSH_BUTTON = 1
End If
Loop
pause 500
serout Oled,baud,(254,1)
pause 10
serout Oled,baud,(254,128)
pause 50
select case w1
case 1
serout Oled,baud,(" yes ")

case 2
serout Oled,baud,("no")

case 3
serout Oled,baud,("maybe")

case 4
serout Oled,baud,("time will tell")

end select
.

Thank you for the help
David

#### hippy

##### Senior Member
The main issue is you never call your 'answer:' code. You probably want to add a "GOSUB answer" after the SERTXD, and also a RETURN at the end of your code, after the END SELECT.

#### nhphantom

##### New Member
Hippy thank you so much for all your help I have a feeling I it would be something simple like a gosub I just can't figure it out and where to would be I did think it would some how have to exit the first loop. Again thank you I find you good at helping with you examples and explanations when needed and yes the gosub did work.

David