Random Numbers on BS2/PBASIC 2.5
HELP!
I have been cruising the net and keep finding other code for older versions....
I want to recreate a die roll 1-6, randomly. Shouldn't this be easy?
Every time I try to code it with the following, I continue to get the same results (not quite random). The following was designed to roll 100 dice and find the distribution between 1-6:
________________________________________________________________________________
' {$STAMP BS2}
' {$PBASIC 2.5}
a var word
c var byte
d var byte
f var byte
g var byte
h var byte
i var byte
j var byte
t var byte
for t = 1 to 100
random a
c= (a//6)+1
if c = 1 then f=f+1
if c = 2 then g=g+1
if c = 3 then h=h+1
if c = 4 then i=i+1
if c = 5 then h=h+1
if c = 6 then j=j+1
if c = 7 then k=k+1
next
debug ? f
debug ? g
debug ? h
debug ? i
debug ? j
__________________________________________________________
When it runs I ALWAYS get the same results:
f = 26
g = 19
h = 27
i = 18
j = 10
Even when I unplug the stamp, close the program, re-open etc... I get the same results. At least I get 100 results.....
Thanx in advance..
I have been cruising the net and keep finding other code for older versions....
I want to recreate a die roll 1-6, randomly. Shouldn't this be easy?
Every time I try to code it with the following, I continue to get the same results (not quite random). The following was designed to roll 100 dice and find the distribution between 1-6:
________________________________________________________________________________
' {$STAMP BS2}
' {$PBASIC 2.5}
a var word
c var byte
d var byte
f var byte
g var byte
h var byte
i var byte
j var byte
t var byte
for t = 1 to 100
random a
c= (a//6)+1
if c = 1 then f=f+1
if c = 2 then g=g+1
if c = 3 then h=h+1
if c = 4 then i=i+1
if c = 5 then h=h+1
if c = 6 then j=j+1
if c = 7 then k=k+1
next
debug ? f
debug ? g
debug ? h
debug ? i
debug ? j
__________________________________________________________
When it runs I ALWAYS get the same results:
f = 26
g = 19
h = 27
i = 18
j = 10
Even when I unplug the stamp, close the program, re-open etc... I get the same results. At least I get 100 results.....
Thanx in advance..
Comments
A better approach might be to hook up a simple light sensing circuit (photoresistor) and use RCTIME to read the value ONCE at start up. Even in similar lighting conditions, this value will never be exactly the same. Use that as your RANDOM seed (your variable a) and you should get better random results.
The idea here is to get some kind of "more" random numbers for the outside world, which can be quite random
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php