base:ax_tinyrand8

This shows you the differences between two versions of the page.

Both sides previous revision Previous revision | |||

base:ax_tinyrand8 [2021-11-28 03:25] wil seeding function further explained |
base:ax_tinyrand8 [2021-11-29 00:12] (current) wil updated seeding function and description |
||
---|---|---|---|

Line 5: | Line 5: | ||

This version stores the seed as arguments and uses self-modifying code and requires only 15 bytes for the random function. The execution time is constant at 18 cycles and much faster than for example a 789-Xorshift. | This version stores the seed as arguments and uses self-modifying code and requires only 15 bytes for the random function. The execution time is constant at 18 cycles and much faster than for example a 789-Xorshift. | ||

- | The seeding function was tricky, since putting any two values into b1 and c1 comes with a 10% risk of ending up in a cycle with shorter period. Therefore, a seeding function is provided that takes an 8 bit values and generates a seed that is guaranteed to be in cycle with a period of 59748. However, there are only 64 different states reachable after seeding. | + | The seeding function was tricky, since putting any two values into b1 and c1 comes with a 10% risk of ending up in a cycle with shorter period. Therefore, a seeding function is provided that takes an 8 bit value and generates a seed that is guaranteed to be in cycle with a period of 59748. Thus, there are 256 different states reachable after seeding. |

In a test, I plotted the output of 51200 random values, which don't seem to reveal any visible patterns: | In a test, I plotted the output of 51200 random values, which don't seem to reveal any visible patterns: | ||

Line 24: | Line 24: | ||

rand8: | rand8: | ||

- | c1=*+1 | + | b1=*+1 |

lda #31 | lda #31 | ||

asl | asl | ||

- | b1=*+1 | + | a1=*+1 |

eor #53 | eor #53 | ||

- | sta c1 | ||

- | adc b1 | ||

sta b1 | sta b1 | ||

+ | adc a1 | ||

+ | sta a1 | ||

rts | rts | ||

+ | ; sets the seed based on the value in A | ||

+ | ; always sets a1 and b1 so that a cycle with maximum period is chosen | ||

+ | ; constants 217 and 21263 have been derived by simulation | ||

set_seed: | set_seed: | ||

pha | pha | ||

- | and #$fa | + | and #217 |

- | sta b1 | + | clc |

+ | adc #<21263 | ||

+ | sta a1 | ||

pla | pla | ||

- | ora #$05 | + | and #255-217 |

- | sta c1 | + | adc #>21263 |

+ | sta b1 | ||

rts | rts | ||

</ | </ |

base/ax_tinyrand8.txt · Last modified: 2021-11-29 00:12 by wil