Re: 8 Bit Random Numbers
- From: Bill Bowden <wrongaddress@xxxxxxx>
- Date: Wed, 4 Feb 2009 19:59:22 -0800 (PST)
On Feb 4, 2:14 am, Nobody <nob...@xxxxxxxxxxx> wrote:
For your original sequence of 3,4,5,7, use 0xB8 or 0x1D, depending upon
the shift direction. The following C code generates the same bit sequence
using both Fibonacci and Galois implementations.
Note that the two versions generate the same bit sequences, but the
sequence of 8-bit states differs.
#include <stdio.h>
typedef unsigned char byte;
byte f_taps = 0x1D; /* 0001 1101 */
byte g_taps = 0xB8; /* 1011 1000 */
byte xor(byte x)
{
x = (x & 0x0F) ^ (x >> 4);
x = (x & 0x03) ^ (x >> 2);
x = (x & 0x01) ^ (x >> 1);
return x;
}
byte f(byte *x)
{
byte t = xor(*x & f_taps);
*x >>= 1;
*x |= t << 7;
return t;
}
byte g(byte *x)
{
byte t = *x & 1;
byte k = (-t) & g_taps;
*x >>= 1;
*x ^= k;
return t;
}
int main(void)
{
byte x = 1, y = 1;
int i;
for (i = 0; i < 0x100; i++) {
byte tx = f(&x);
byte ty = g(&y);
printf("%d %d\n", tx, ty);
}
return 0;
}
Thanks for the C code, but my knowledge of C is limited, and it may be
a little slow to implement.
I found an assembly program for a PIC processor on the web which
requires 10 lines of code, or maybe 44 clock cycles.
The idea is to shift the random register left and obtain the result in
the working register (w) and then shift the register again which rolls
around the carry bit to the first bit, again in (w). Then the three
feedback bits are tested to determine the new input bit and the result
fed to the lowest bit.
Looks pretty speedy, but I was thinking the parallel approach might
reduce the 44 clocks to a lower number.
What I'm not understanding with the parallel approach is how the 8 bit
result from xoring a constant 8 bits is converted to a single bit to
be shifted into the register. In other words, if I xor
B8 with the contents of the random register, I get some new 8 bit
number. What can I do with that? How does the new 8 bit result resolve
to a single bit?
Main:
RLF RANDOM,0
RLF RANDOM,0
BTFSC RANDOM,3
XORLW 1
BTFSC RANDOM,4
XORLW 1
BTFSC RANDOM,5
XORLW 1
MOVWF RANDOM
GOTO Main
-Bill
.
- Follow-Ups:
- Re: 8 Bit Random Numbers
- From: Nobody
- Re: 8 Bit Random Numbers
- From: Bob Masta
- Re: 8 Bit Random Numbers
- References:
- Re: 8 Bit Random Numbers
- From: Bill Bowden
- Re: 8 Bit Random Numbers
- From: nospam
- Re: 8 Bit Random Numbers
- From: John Fields
- Re: 8 Bit Random Numbers
- From: Nobody
- Re: 8 Bit Random Numbers
- From: John Fields
- Re: 8 Bit Random Numbers
- From: Nobody
- Re: 8 Bit Random Numbers
- From: John Fields
- Re: 8 Bit Random Numbers
- From: Nobody
- Re: 8 Bit Random Numbers
- From: Bill Bowden
- Re: 8 Bit Random Numbers
- From: nospam
- Re: 8 Bit Random Numbers
- From: Bill Bowden
- Re: 8 Bit Random Numbers
- From: Nobody
- Re: 8 Bit Random Numbers
- Prev by Date: Basic computer knowledge
- Next by Date: Re: 8 Bit Random Numbers
- Previous by thread: Re: 8 Bit Random Numbers
- Next by thread: Re: 8 Bit Random Numbers
- Index(es):
Relevant Pages
|