Exact match. Not showing close matches.
PICList
Thread
'[PIC]: How to generate multiple random numbers?'
2001\10\13@204703
by
solar
2001\10\13@223111
by
Ashley Roll

Hi Ashley,
An Electronic Dice Roller hey? :)
Generating _Truly_ random number is very difficult and requires special
hardware, however there are some simple "cop outs" that will be fine for
your application. I'm getting this info from a cryptography text called
"Applied Cryptography" by Bruce Schneier which looks at them a bit.
What your after is called a "PseudoRandom Sequence Generator", there are
several of these varying in "randomness" and complexity.
I'd suggest that you use a "Linear Feedback Shift Register". These work by
maintaining a shift register and rotating it get an output bit and then
feeding back (using XOR) some of the bits back into the register. If you
choose the correct bits, you can mathematically determine the "length" of
the sequence before it repeats. (complex math involved which I make no claim
of understanding, but fortunately there are tables of examples to use :)
The problem is that a LFSR is not efficient to implement in software unless
you change the way you do the do the feedback to what is called a "Galois
Configuration". It achieves the same results.
The basic format of the code is (note I haven't tested this):
ClockLFSR: MOVLW 089H ; The Feedback Polynomial
BCF STATUS,C ; Clear the carry flag to shift in a 0
RRF LFSR_ShiftReg ; Rotate the shift register
BTFSS STATUS,C ; if we rotated out a 1, we need to XOR
RETLW 0 ; we rotated out a 0, return it
XORWF LFSR_ShiftReg, F ; Do the XOR
RETLW 1 ;return the 1
To use, when you start up, initialise LFSR_ShiftReg to any value except
Zero. and call ClockLFSR to return a single bit at a time.
I'd avoid using LFSR_ShiftReg as the 8 bit output as this isn't very good.
So, to roll a D6, you need 3 bits to give you a number between 0 and 7 which
you need to get the remainder after dividing by 5 (modulo operation). to
give you a number between 0 and 5 (mapping to 1 and 6 on your dice.)
The next question is how do you do a Modulo on a PIC :)
Hope this helps.
Ash.

Ashley Roll
Digital Nemesis Pty Ltd
http://www.digitalnemesis.com
Mobile: +61 (0)417 705 718
> {Original Message removed}
2001\10\13@230610
by
Ashley Roll

Actually, I just had a thought..
To generate a dice roll, say a D6, arrange a counter and start it at 0,
Then generate a bit (using the LFSR from my previous post), if it is one,
increment the counter, if it is 0 then leave the counter alone. Check the
counter is not greater then 5, if it is then reset it to zero. Do this many
times. Then use the number in the counter as your roll.
The problem is how many times to "clock" the counter. Too few and you will
get many more "lower" numbers then "higher" ones. Too many times and it will
take forever. There is also a concern about the period of the LFSR it can
only have 255 combinations before it repeats and this could affect your
numbers generated. It is possible to use a bigger LFSR (16, or 32 bits wide)
by altering the code a bit and using a different polynomial (eg 0x10000009
for a 32 bit register). Also, I'd be clocking the LFSR even when your not
using it so you introduce more "randomness" from the time between when the
user presses the buttons to roll a dice.
Note that you have to go through a few times because about 1/2 the bits will
be 1 and the other half will be 0, therefore it will be more likely to
generate a "3" then either a "0" or a "6" (whereas it is equally likely on a
dice).
I'd try running through about 4 times (4 * 6 = 24 bits) and see how that
goes. This might be ok for a D4 (4 * 4 = 16 bits) to a D10 (4 * 10 = 40
bits) or even a D20, but I'd generate a D100 as 2 D10s :) It might be
worthwhile having the PIC send out the bytes to a PC using a serial port and
log it in a program and count the number of each value rolled to make sure
they are coming out about as often. You might need to adjust the number of
bits you use to generate the roll for each different type of dice.
hope this helps more :)
Ash.

Ashley Roll
Digital Nemesis Pty Ltd
http://www.digitalnemesis.com
Mobile: +61 (0)417 705 718

http://www.piclist.com hint: To leave the PICList
.....piclistunsubscriberequestKILLspam@spam@mitvma.mit.edu
2001\10\14@083418
by
Olin Lathrop
> To generate a dice roll, say a D6, arrange a counter and start it at 0,
>
> Then generate a bit (using the LFSR from my previous post), if it is one,
> increment the counter, if it is 0 then leave the counter alone. Check the
> counter is not greater then 5, if it is then reset it to zero. Do this
many
> times. Then use the number in the counter as your roll.
It seems the problem comes down to generating a random number from 0 to N,
where N+1 is not a power of 2. I have a routine that does this on a 16F876.
If first finds the mask for all the bits in the number. In other words, it
finds the smallest 2**K1 that is greater than or equal to N. You now get a
random binary number and AND this mask to it. If the result is greater than
N, throw it out and try again. The worst case average is you have to throw
out 1 of every 2 random numbers. The source code is in HAL_RAND.ASPIC at
http://www.embedinc.com/pic/hal.htm.
********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 7429014, olinKILLspamembedinc.com, http://www.embedinc.com

http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: >uP ONLY! [EE]:,[OT]: >Other [BUY]:,[AD]: >Ads
2001\10\14@195409
by
Peter L. Peres
What is a 'multiple random number' ? Normally the algorythms that produce
random numbers generate it in binary and you need to convert it for
display. You can implement an algorythm as complex as you wish (until you
run out of device space).
Peter

http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: >uP ONLY! [EE]:,[OT]: >Other [BUY]:,[AD]: >Ads
2001\10\16@121443
by
Harold M Hallikainen
More... (looser matching)
 Last day of these posts
 In 2001
, 2002 only
 Today
 New search...