'Random number generation'
I need some code or a algorithm for generating a 8 bit random number on
a PIC. Does anyone have any code or an algorithm to accomplish this?
P.S. I need working code or a working algorithm, not theory. Thanks.
I been off the list for a while so I apologize if I am reposting
|In "PIC your personal introductory course" John Morton suggests
simply adding h'27' repetitively. I think that the result won't be quite
random, but it does cycle through a long cycle without repeats.
Most Pseudo Random numbers require multiplication (or iterative addition)
to accomplish. The main requirement is to find a "magic number" that not
only maximizes runtimes without repeats but also avoids easy analysis as a
"SERIES" so that it is easier to avoid prediction. This might be harder on
a "LOW END PIC" than some of the other options. One way of further
confusing the issue, is to use a "RANDOM SEED" such as a random number
created by a different pseudo random number generator, to generate the
base that you multiply/add to etc, every so often.
GRAEME SMITH email: freenet.edmonton.ab.cagrysmith
Address has changed with little warning!
(I moved across the hall! :) )
Email will remain constant... at least for now.
On Sun, 31 Jan 1999, David Sprenkle wrote:
At 06:11 PM 1/31/99 -0800, David Sprenkle wrote:
>I need some code or a algorithm for generating a 8 bit random number on
>a PIC. Does anyone have any code or an algorithm to accomplish this?
>P.S. I need working code or a working algorithm, not theory. Thanks.
>I been off the list for a while so I apologize if I am reposting
I have code for the AVR8515 which you could easily translate, for a maximal
length pseudorandom number generator.
You won't find cryptographically random cheap.
You won't find provably unpredictable cheap.
The PN generator can give you anything up to it's length -1 with all
possible values equally distributed, but of you use the full length, then
the all zeroes state can't occur. (the generator would lock up if you
initted it to that value)
You can pick and choose your bits, invert them, so the output is not easily
recognizable as a fixed pattern.
It really depends on what you want to use it for..
Which algorythm you choose also depends upon what the resulting number will
be used for. If it is for noise generation, it probably doesn't need to be
all that random. If it is for a random delay in a communications system,
likewise, I don't think it would need to be really very random, just enough
that the system won't get locked into a cetain repetitive state. However,
if it is for security or for picking lottery numbers,bingo, etc, it better
be pretty random.
At 07:39 PM 1/31/99 -0700, you wrote:
| Sean Breheny |
| Amateur Radio Callsign: KA3YXM |
| Electrical Engineering Student |
Save lives, please look at http://www.all.org
Personal page: http://www.people.cornell.edu/pages/shb7
cornell.edu Phone(USA): (607) 253-0315 ICQ #: 3329174 shb7
'Random number generation'
w. v. ooijen / f. hanneman
|> I need some code or a algorithm for generating a 8 bit random number on
> a PIC. Does anyone have any code or an algorithm to accomplish this?
Random (actually pseudo-random!) numbers are useually generated by
a shift register with XOR feedback. You can vary the length of the
SR to get a suitable cyclus length.
The jal compiler has a such a reandom library which is so small that
I've attched it below. You should be able to translate this into assembler
(in fact is it almost assembler). I deleted the copyright notice, but it
is covered by the GNU LGPL so unless you want to sell a library based
upon it you can use it freely. When you are too lazy to translate
the code below into assembler you could download the jal compiler
and let it do the job. (http://www.xs4all.nl/~wf/wouter/pic/jal)
var byte _random_b1
var byte _random_b2
var byte _random_b3
-- make sure there is at least one bit set!
asm bsf _random_b1, 0
procedure _random_shift is
-- from the art of electronics, p657:
-- 24 bits LFSR needs xor feedback from taps at 17, 22 and 23
btfsc _random_b3, 1
btfsc _random_b3, 6
btfsc _random_b3, 7
rlf _random_b1, f
rlf _random_b2, f
rlf _random_b3, f
var bit _random_bit at _random_b1 : 0
var byte _random_byte at _random_b1
procedure randomize( byte in n = 0 ) is
_random_b1 = n
_random_b2 = 1
_random_b3 = 0
for 24 loop
function random_bit return bit is
function random_byte return byte is
for 8 loop
If you are considering a Pic17Cxx have a look at AN544 in the embedded
Even if you are using a 16C the algorithm should be easily implemented - it
basically involves boolean operations and shifting on 16 bits.
More... (looser matching)
- Last day of these posts
- In 1999
, 2000 only
- New search...