Random Number
-
- Posts: 25
- Joined: 11 Feb 2005 17:50
about the random generator
sub procedure DO_RANDOM(dim min as word, dim max as word)
RANDOM = byte(RANDOM) ' force bank setting for the following assembly
do
asm
RLF MAIN_GLOBAL_RANDOM,W
RLF MAIN_GLOBAL_RANDOM,W
BTFSC MAIN_GLOBAL_RANDOM,4
XORLW 1
BTFSC MAIN_GLOBAL_RANDOM,5
XORLW 1
BTFSC MAIN_GLOBAL_RANDOM,3
XORLW 1
MOVWF MAIN_GLOBAL_RANDOM
end asm
loop until ((RANDOM < max) AND (RANDOM > min))
end sub
....
DO_RANDOM(0,6)
colore = RANDOM
...
I just changed COLORE to PORTB, with randomgen as my program name. I get -- 0:0 0 Linker error: randomgen_L_1o: label not found. What does it mean?
RANDOM = byte(RANDOM) ' force bank setting for the following assembly
do
asm
RLF MAIN_GLOBAL_RANDOM,W
RLF MAIN_GLOBAL_RANDOM,W
BTFSC MAIN_GLOBAL_RANDOM,4
XORLW 1
BTFSC MAIN_GLOBAL_RANDOM,5
XORLW 1
BTFSC MAIN_GLOBAL_RANDOM,3
XORLW 1
MOVWF MAIN_GLOBAL_RANDOM
end asm
loop until ((RANDOM < max) AND (RANDOM > min))
end sub
....
DO_RANDOM(0,6)
colore = RANDOM
...
I just changed COLORE to PORTB, with randomgen as my program name. I get -- 0:0 0 Linker error: randomgen_L_1o: label not found. What does it mean?
-
- Posts: 137
- Joined: 10 Mar 2005 22:59
- Location: Cardiff, UK
- Contact:
RND keyword
Just a suggestion for the next MB now that 4.0 is out the door.
Other Basics often have an RND keyword for a random number generator. How about providing the code discussed here as a built-in language feature?
For example, for BBC micro basic (which you needn't copy exactly)
RND
A function which returns a random number. The type and range of the number returned depends upon the optional parameter.
RND returns a random integer 1 - &FFFFFFFF.
RND(n) returns an integer in the range 1 to n (n>1).
RND(1) returns a real number in the range 0.0 to .99999999.
If n is negative the pseudo random sequence generator is set to a number based on n and n is returned.
Other Basics often have an RND keyword for a random number generator. How about providing the code discussed here as a built-in language feature?
For example, for BBC micro basic (which you needn't copy exactly)
RND
A function which returns a random number. The type and range of the number returned depends upon the optional parameter.
RND returns a random integer 1 - &FFFFFFFF.
RND(n) returns an integer in the range 1 to n (n>1).
RND(1) returns a real number in the range 0.0 to .99999999.
If n is negative the pseudo random sequence generator is set to a number based on n and n is returned.
Thanks
EDIT: Well this code locks up the 16f88.
And this is my code to call it.
EDIT: Well this code locks up the 16f88.
Code: Select all
sub procedure DO_RANDOM(dim min as word, dim max as word)
RANDOM = byte(RANDOM) ' force bank setting for the following assembly
do
asm
RLF MAIN_GLOBAL_RANDOM,W
RLF MAIN_GLOBAL_RANDOM,W
BTFSC MAIN_GLOBAL_RANDOM,4
XORLW 1
BTFSC MAIN_GLOBAL_RANDOM,5
XORLW 1
BTFSC MAIN_GLOBAL_RANDOM,3
XORLW 1
MOVWF MAIN_GLOBAL_RANDOM
end asm
loop until ((RANDOM < max) AND (RANDOM > min))
end sub
Code: Select all
sub procedure pRandom
DO_RANDOM(1,100)
PORTA = 255
vDelay_ms(Random)
DO_RANDOM(1,100)
PORTA = 0
vDelay_ms(Random)
end sub
Since you are using PORTA with a 16F88 you should turn your ADC's off with ANSEL=0. Also, Vdelay_ms() does not accept values below 11. Try the following as a demo:
Code: Select all
sub procedure DO_RANDOM(dim min as byte, dim max as byte)
Do
asm
RLF TMR1H,W
RLF TMR1H,W
BTFSC TMR1H,4
XORLW 1
BTFSC TMR1H,5
XORLW 1
BTFSC TMR1H,3
XORLW 1
MOVWF TMR1H
end asm
Loop Until (TMR1H < max) AND (TMR1H > min)
end sub
sub procedure pRandom
DO_RANDOM(10, 100)
PORTA = 255
vDelay_ms(TMR1H)
DO_RANDOM(10, 100)
PORTA = 0
vDelay_ms(TMR1H)
end sub
main:
ANSEL = 0
TRISA = 0
TMR1H = 1
while 1=1
pRANDOM
wend
end.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]
[url=http://circuit-ed.com]CircuitED -[/url]
Can anyone write a random function based on the code sample here, without any init that can effect the main project (without TRISB etc...)
just a simple random function such as this:
Thanks
Vneto
just a simple random function such as this:
Code: Select all
dim value as word
value = random(min, max)
Vneto
Okay....you get your wish...complete with minimum and maximum limits. I also think this new code (I grabbed it from the PicList) is even a better random generator than I posted before. This is a module so that if you're using 16F's or 18F's it has a specifc function for you.
Here is the module to include with your program:
Here is an example using the module for an 18F PIC. Use rnd8_16F for 16F PIC's.
Here is the module to include with your program:
Code: Select all
module rngen
implements
sub function rnd8_16F(dim min, max as byte) as byte
dim f0 as boolean
f0 = true
While f0
asm
clrf STATUS
rlf TMR2, 1
swapf TMR2, 0
andlw 0xE0
rrf TMR2, 1
addwf TMR2, 0
addwf TMR2, 0
addwf TMR2, 0
sublw 0x35
movwf TMR2
end asm
If (TMR2 <= max) And (TMR2 >= min) Then f0 = false End If
Wend
result = TMR2
end sub
sub function rnd8_18F(dim min, max as byte) as byte
dim f0 as boolean
f0 = true
While f0
asm
clrf STATUS
rlcf TMR2, 1
swapf TMR2, 0
andlw 0xE0
rrcf TMR2, 1
addwf TMR2, 0
addwf TMR2, 0
addwf TMR2, 0
sublw 0x35
movwf TMR2
end asm
If (TMR2 <= max) And (TMR2 >= min) Then f0 = false End If
Wend
result = TMR2
end sub
end.
Code: Select all
include "rngen"
dim value as byte
main:
PORTB = 0
PORTD = 0
TRISB = 0
TRISD = 0
While true
value = rnd8_18F(0, 255)
latb = value
delay_ms(200)
Wend
end.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]
[url=http://circuit-ed.com]CircuitED -[/url]