PDA

View Full Version : random numbers 1-4



tekdavid
- 29th March 2005, 23:59
in picbasic standard how would you generate a random number 1 - 4 then make leds 1 -4 light up in that order i have been unable to get it working just right i though some on out the could give me some new thoughts on how to do that.

mister_e
- 30th March 2005, 05:30
look those thread... more than you need... good luck

http://www.picbasic.co.uk/forum/showthread.php?t=885&highlight=random

http://www.picbasic.co.uk/forum/showthread.php?t=937&highlight=random

http://www.picbasic.co.uk/forum/showthread.php?t=886&highlight=random

Melanie
- 30th March 2005, 10:56
Everytime you use Random, you in effect seed a new value in the word variable RandomValue. You only want the bottom two bits (to give you your four possible values) so extract them... Use those bits to illuminate your LED's... simple...



LEDA var PortB.0
LEDB var PortB.1
LEDC var PortB.2
LEDD var PortB.3

RandomValue var WORD
LEDValue var BYTE

Start:
Low LEDA
Low LEDB
Low LEDC
Low LEDD

Random RandomValue
LEDValue=RandomValue & $03

If LEDValue=0 then High LEDA
If LEDValue=1 then High LEDB
If LEDValue=2 then High LEDC
If LEDValue=3 then High LEDD

Pause 1000
Goto Start


This will give you one-of-four Random selection forever. I haven't got PBC but I'm sure you could convert it simply...

Melanie
- 30th March 2005, 11:05
OK... looking at the PBC manual... here's the conversion... sombody can correct me if I've made a mistake...



' LEDA is on PortB.0
' LEDB is on PortB.1
' LEDC is on PortB.2
' LEDD is on PortB.3

' RandomValue is variable W0
' LEDValue is variable B2

Start:
Low 0
Low 1
Low 2
Low 3

Random W0
B2=W0 & $03

If B2=0 then High 0
If B2=1 then High 1
If B2=2 then High 2
If B2=3 then High 3

Pause 1000
Goto Start
Well, actually, you could probably reduce it further...



Start:
Low 0
Low 1
Low 2
Low 3

Random W0
B2=W0 & $03

High B2

Pause 1000
Goto Start