Searching \ for 'Urgent: Random No Generator Needed' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/method/math.htm?key=random
Search entire site for: 'Urgent: Random No Generator Needed'.

Truncated match.
PICList Thread
'Urgent: Random No Generator Needed'
1997\01\07@141958 by Tim Kerby

picon face
I am about to reach a deadline on a project at school (for the engineering
scheme scotland) and my random no generation isnt working.  It must take the
tmr0 or any internal part of the chip as i dont have lines to spare and give
out a number between 1 and 12 (inclusive).

Please help

Tim

1997\01\07@151732 by fastfwd

face
flavicon
face
Tim Kerby <spam_OUTPICLISTTakeThisOuTspamMITVMA.MIT.EDU> wrote:

> I am about to reach a deadline on a project at school (for the
> engineering scheme scotland) and my random no generation isnt
> working.  It must take the tmr0 or any internal part of the chip as
> i dont have lines to spare and give out a number between 1 and 12
> (inclusive).

Tim:

To select a TMR0-based number between 1 and 12, inclusive, do
something like this:

   LOOP:       MOVF    TMR0,W
           ANDLW   00001111B
           ADDLW   -12
           BC      LOOP
           ADDLW   13

After executing the fragment above, W will contain a number in the
range [1-12].  This is just about the worst possible way to get
random numbers -- the distribution is VERY skewed toward the low end
of the range, and the whole thing's based on a deterministic timer --
but it's quick (so long as you haven't enabled a large TMR0
prescaler), easy, and doesn't require any additional file registers.

-Andy

=== Andrew Warren - .....fastfwdKILLspamspam@spam@ix.netcom.com                 ===
=== Fast Forward Engineering - Vista, California          ===
===                                                       ===
=== Did the information in this post help you?  Consider  ===
=== contributing to the PICLIST Fund.  Details are at:    ===
=== http://www.geocities.com/SiliconValley/2499/fund.html ===

1997\01\07@153402 by Tim Kerby

picon face
The prescaler is at maximum but the clock is 20MHz.  I will include a loop
to give a new number if the number chosen was the same as the last which
should help as only an even distribution of nos is needed and not a true
random.  The code looks good.  Mine got the timer value and counted
subtractions of a number and the code was huge.  Is it the timer which is
skewed or the way the code is implemented?

Thanks

Tim




{Quote hidden}

1997\01\07@161114 by fastfwd

face
flavicon
face
Tim Kerby <.....PICLISTKILLspamspam.....MITVMA.MIT.EDU> wrote:

> The prescaler is at maximum but the clock is 20MHz.  I will include
> a loop to give a new number if the number chosen was the same as the
> last which should help as only an even distribution of nos is needed
> and not a true random.  The code looks good.  Mine got the timer
> value and counted subtractions of a number and the code was huge.
> Is it the timer which is skewed or the way the code is implemented?

Tim:

It's the way that I implemented the code... Guess I should have
included some comments.  Here's a documented version:

   LOOP:   MOVF    TMR0,W      ;Grab the timer value (0-255).

           ANDLW   00001111B   ;Mask off all but the lower nibble,
                               ;so W is now in the range [0-15].

           ADDLW   -12         ;Is W > 11?
           BC      LOOP        ;If so, loop back and try again;
                               ;TMR0 will soon increment to the
                               ;point where its low nibble will be
                               ;equal to 0 again.

           ADDLW   13          ;Otherwise, W is in the range
                               ;[-12 to -1]; add 13 to bring it
                               ;into the range [1-12].

As you can see, the returned value has an even distribution from 2 to
12, but it'll contain "1" 5 times as often as any other number.

If you can spare a register, you can get a more-even distribution by
using a linear-feedback shift-register as your basis, rather than
TMR0.  Here's a method that uses Marv Isaacman's LFSR code from my
company's web page:

   RANDOM  EQU     [some file register]

           ....

           MOVLW   1           ;Seed the random-number generator
           MOVWF   RANDOM      ;(your program only needs to do this
                               ;once).

           ....

   LOOP:   MOVLW   01DH        ;RANDOM holds the next number in the
           CLRC                ;pseudo-random sequence.
           RLF     RANDOM      ;
           SKPNC               ;
           XORWF   RANDOM      ;

           MOVF    RANDOM,W    ;Grab the random number (0-255).

           ANDLW   00001111B   ;Mask off all but the lower nibble,
                               ;so W is now in the range [0-15].

           ADDLW   -12         ;Is W > 11?
           BC      LOOP        ;If so, loop back and try again with
                               ;the next number in the sequence.

           ADDLW   13          ;Otherwise, W is in the range
                               ;[-12 to -1]; add 13 to bring it ;into
                               the range [1-12].

           ; At this point, W is in the range [1-12].

Hope this helps...

-Andy

=== Andrew Warren - EraseMEfastfwdspam_OUTspamTakeThisOuTix.netcom.com                 ===
=== Fast Forward Engineering - Vista, California          ===
===                                                       ===
=== Did the information in this post help you?  Consider  ===
=== contributing to the PICLIST Fund.  Details are at:    ===
=== http://www.geocities.com/SiliconValley/2499/fund.html ===

1997\01\08@140151 by Tim Kerby
picon face
Andy
Thanks for such an efficient reply.  I forgot to say i was using the 16c74
so I have hundreds of free registers.  Better get back to the coding.

Thanks
Tim

More... (looser matching)
- Last day of these posts
- In 1997 , 1998 only
- Today
- New search...