| ||
Shopping Cart Download Website |
Home | Products | Teensy | Blog | Forum |
You are here: 8051 Tools Code Library Random Numbers |
|
Pseudo Random Number GeneratorThese routines generate a sequence of pseudo-random numbers using the ordinary linear feedback shift register technique. Both routines require memory to hold the last number generated. They produce a predictable but randomly distributed sequence of numbers, similar to the rand() function in most C libraries.No routine analogous to srand() is provided, but it is easily implemented by writing the seed value into the memory which holds the previously generated number. These routines check for zero (the illegal value), so it is not necessary to initialize the memory with a seed value unless a predictable starting seed is required or a truely random number is available.
Some people have asked about the mysterious "p" variable. This line
mov c, p utilizes a special feature of the 8051 processor. The "p" bit is one of the bits in the PSW (program status word) special functon register. This bit is the parity of the byte currently stored in the accumulator. This is equivilant to the exclusive OR of all 8 bits of the accumulator. The code uses this feature to quickly compute the exclusive OR of 4 bits by using a logical AND to mask off the other 4 bits, then simply read the parity bit to get the exclusive OR of the 4 remaining bits. |