http://channelf.se/veswiki/index.php?title=Snippet:Pseudorandom_numbers&feed=atom&action=historySnippet:Pseudorandom numbers - Revision history2021-09-18T10:04:34ZRevision history for this page on the wikiMediaWiki 1.28.0http://channelf.se/veswiki/index.php?title=Snippet:Pseudorandom_numbers&diff=554&oldid=prevE5frog: Created page with "Sometimes you want to add a random number to your programming but you will have to make your own code to produce it. <br> A common way to make a pseudo-random number generator..."2020-01-03T11:56:30Z<p>Created page with "Sometimes you want to add a random number to your programming but you will have to make your own code to produce it. <br> A common way to make a pseudo-random number generator..."</p>
<p><b>New page</b></p><div>Sometimes you want to add a random number to your programming but you will have to make your own code to produce it. <br><br />
A common way to make a pseudo-random number generator (PRNG) is do use a Linear Feedback Shift Register (LFSR). <br><br />
This will give you a series of numbers from 0 to 255 in a seemingly randomized order. <br><br />
<br />
You will however get the same series of values in the same order every time, your "seed" number will choose the start position.<br> <br />
<br />
<br />
Here's an example of a subroutine with 8-bit LFSR:<br />
<br />
<pre><br />
; In this example the pseudo random number is stored in register 1<br />
; Also uses register 0 for temporary storage<br />
<br />
LFSR: ; Linear Feedback Shift Register<br />
clr<br />
as 1<br />
bz doEor<br />
lr 0, A ; Save original number in r0<br />
sl 1 ; shift one step left<br />
lr 1, A<br />
bz noEor<br />
<br />
; Do XOR if b7 was 1, workaround for missing carry on "sl" <br />
lr A, 0<br />
ni %10000000<br />
bz noEor<br />
<br />
doEor:<br />
lr A, 1<br />
xi $2b ; Do the XOR thing<br />
lr 1, A<br />
noEor:<br />
pop<br />
<br />
</pre><br />
<br />
The value used with XOR-function is $2B here, one of these other values can be used to give a different 256 byte non-repeating chain of values. <br><br />
$1d, $2b, $2d, $4d, $5f, $63, $65, $69, $71, $87, $8d, $a9, $c3, $cf, $e7, $f5<br><br />
<br />
When using $2B as XOR value the data will be this:<br><br />
[[File:LFSR_%242B.png]] <br />
<br />
<br><br />
When using $71 as XOR value the data will be this:<br><br />
[[File:LFSR_%2471.png]] <br />
<br />
<br><br />
[https://codebase64.org/doku.php?id=base:small_fast_8-bit_prng CodeBase64 used for reference]</div>E5frog