RANDOM command blues

[code]LED1 con P15 ; Red LED
LED2 con P12 ; Green LED
LED3 con P8 ; Yellow LED
result VAR Word
Main
result = 1100
RANDOM result
gosub TurnLEDsOn
gosub TurnLEDsOff
goto main
End

TurnLEDsOn ; LED on subroutine
pause result
high LED1
pause result
high LED2
pause result
high LED3
return

TurnLedsOff ; LED off subroutine
Low LED1
pause result
Low LED2
pause result
Low LED3
pause result
return[/code]

This is the faulty code I’ve been using. The RANDOM command while being in the manuals, help files, etc. does not work. It is not even recognized as a command according to the error line.

Can someone give me a hand?

Which processor and IDE are you using?

As far as I know (and I’m not a programmer), RANDOM should be used like this:

result random seed

where “result” is a var where the random number will be stored, and “seed” is a var containing what the random command uses to generate the result.

If you wanted to use your “result” variable to serve as both seed and result, I believe you’d have to write it like this:

result random result

I got this information from the BM Wiki here, and there’s a bit more in-depth information, as well:
basicmicro.wikia.com/wiki/MBasic … ons#RANDOM

It’s actually kinda funny what one learns by transcribing information. :smiley:

Hope this helps, but if it doesn’t… Remember, I’m not a programmer! ;D

That’s the gist of it. If done like below the new result seeds the next generation.

variable = some starting seed value main variable = random variable goto main

Here is a working version of your code. It’s pausing random intervals between turning on and then turning off the leds.

[code]LED1 con P12 ; Red LED
LED2 con P13 ; Green LED
LED3 con P14 ; Yellow LED
result VAR Word
smaller var byte
result = 500

Main
result = RANDOM result
smaller = result / 256
; serout s_out,i38400,"result= ", dec result, "smaller= ", dec smaller, 13]
gosub TurnLEDsOn
gosub TurnLEDsOff
goto main
End

TurnLEDsOn ; LED on subroutine
pause result
high LED1
pause result
high LED2
pause result
high LED3
return

TurnLedsOff ; LED off subroutine
Low LED1
pause result
Low LED2
pause result
Low LED3
pause result
return[/code]

Here is another way to do it. Random, but sequential…

[code]LED1 con P12 ; Red LED
LED2 con P13 ; Green LED
LED3 con P14 ; Yellow LED
result VAR Word
smaller var byte
smallest var bit
temp var byte
result = 500

for temp = 12 to 14
low temp
next

Main

; serout s_out,i38400,"result= ", dec result, "smaller= ", dec smaller, "smallest= ", dec smallest, 13]
for temp = 12 to 14
gosub getrandom
toggle temp
pause smaller
next

goto main

getrandom
result = RANDOM result ; result = (0 - 65535)
smaller = result / 256 ; smaller = (0 - 255)
smallest = smaller//2 ; temp = (0 - 1) 0 = even, 1 = odd
return[/code]

However if you want to have them blinking at random…

[code]LED1 con P12 ; Red LED
LED2 con P13 ; Green LED
LED3 con P14 ; Yellow LED
result VAR Word
smaller var byte
smallest var bit
temp var byte
result = 500

for temp = 12 to 14
low temp
next

Main

for temp = 12 to 14
gosub getrandom
if smallest then
toggle temp
endif
pause smaller
next

goto main

getrandom
result = RANDOM result ; result = (0 - 65535)
smaller = result / 256 ; smaller = (0 - 255)
smallest = smaller//2 ; temp = (0 - 1) 0 = even, 1 = odd
return[/code]

thanks for the replys, seems i didn’t understand how to use random after all. This is exactly what i am looking for.

result = RANDOM result … who knew i needed another result.

can you explain what this section means?

thanks

You don’t always need to use the same variables there. You could follow this pattern:

** var1 = RANDOM var2**

If you follow the pattern of var1 = RANDOM var1, it just makes the seed randomized, which helps the end result be more random.

The “getrandom” subroutine will do the following:

** result = RANDOM result** ; result = (0 - 65535)
Use the value in the “result” variable as a RANDOM seed, and store the new random number in “result”. The value of “result” after randomization will be between 0-65535. “result” is a word-size variable.

** smaller = result / 256** ; smaller = (0 - 255)
Divide the random number stored in “result” by 256, in order to reduce the size from a word-size variable to a byte-size variable. Store the resulting smaller random number in the variable “smaller”.

The program uses the value in “smaller” as the duration an LED is on or off.

** smallest = smaller//2** ; temp = (0 - 1) 0 = even, 1 = odd
Here, we use the modulo (//) function to tell if the random number is odd or even, and reduce the random number down to either a 0 or 1. The mod division function returns the remainder after division. For example, in pseudocode:

** 5 // 2 = 1**

Since the remainder is 1, we know the original number is Odd.

The last program uses the value in “smallest” to randomize the LED sequence, in this bit of code:

if smallest then toggle temp endif

Hope this helps, and doesn’t confuse! :laughing:

yes this helped a great deal, thank you. :slight_smile:

I only had troubles with understanding smaller and smallest, but you also provided some extra information on Var1 = Var1.

Thanks again :wink:

No problem! I like to err on the side of over-explaining… :smiley: