Random Number

General discussion on mikroBasic.
Author
Message
xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#16 Post by xor » 03 Feb 2006 22:11

If you're running the exact original program you will see any number of bits output and change every 1/2 second. What PIC are you using?
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

pointser
Posts: 62
Joined: 07 Jan 2006 13:53
Location: Casalpusterlengo, Italy

#17 Post by pointser » 21 Feb 2006 16:20

I use the function above on PIC16F84A

apochrypha
Posts: 25
Joined: 11 Feb 2005 17:50

about the random generator

#18 Post by apochrypha » 14 Mar 2006 16:05

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?

ralphrmartin
Posts: 137
Joined: 10 Mar 2005 22:59
Location: Cardiff, UK
Contact:

RND keyword

#19 Post by ralphrmartin » 14 Mar 2006 20:30

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.

hcker2000
Posts: 192
Joined: 22 Feb 2006 08:45
Location: Ohio
Contact:

#20 Post by hcker2000 » 28 Mar 2006 15:22

I will have to give this code a try on my 16f88's and see what I get.

hcker2000
Posts: 192
Joined: 22 Feb 2006 08:45
Location: Ohio
Contact:

#21 Post by hcker2000 » 30 Mar 2006 05:22

How could i use this code with Delay_ms. I tryed Delay_ms(Random) but compiler didnt like that.

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#22 Post by xor » 30 Mar 2006 11:25

Look at the Delays library in HELP in the compiler. There are 2 of the 4 that can accept variable parameters, Vdelay_ms() and Delay_cyc(). Delay_us() and Delay_ms() only accept literals.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

hcker2000
Posts: 192
Joined: 22 Feb 2006 08:45
Location: Ohio
Contact:

#23 Post by hcker2000 » 30 Mar 2006 13:29

Thanks


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
And this is my code to call it.

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

hcker2000
Posts: 192
Joined: 22 Feb 2006 08:45
Location: Ohio
Contact:

#24 Post by hcker2000 » 31 Mar 2006 23:34

Any help?

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#25 Post by xor » 01 Apr 2006 03:02

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]

hcker2000
Posts: 192
Joined: 22 Feb 2006 08:45
Location: Ohio
Contact:

#26 Post by hcker2000 » 02 Apr 2006 03:20

Ahh that must be it. I already have the ANSEL=0 ;) so its probably the less than 11

vneto
Posts: 94
Joined: 09 Jan 2006 18:37

#27 Post by vneto » 22 Apr 2006 17:27

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:

Code: Select all

dim value as word
value = random(min, max)
Thanks
Vneto

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#28 Post by xor » 22 Apr 2006 20:13

It's easy enough to do if value = a byte instead of a word. The routine is set up for a byte return. If that's okay then I can rewrite it and create a function.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

vneto
Posts: 94
Joined: 09 Jan 2006 18:37

#29 Post by vneto » 22 Apr 2006 21:15

xor

That will be great if you can rewrite the function without any Dependencies
and return a Byte

Thanks

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#30 Post by xor » 23 Apr 2006 02:52

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:

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.
Here is an example using the module for an 18F PIC. Use rnd8_16F for 16F PIC's.

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]

Post Reply

Return to “mikroBasic General”