Libraryless. Click here for Pure Java version (7975L/45K).
// maximum for n is 31 or 32 // (32 works but makes things a bit funky because you'll see negative // integers as value, step count and cycle length. Unless you call the // ...AsLong methods.) // TODO: find tap tables for n=25 through 32 // (so for now max is 24) sclass SimpleLSFR { // params int n; // max 32 int start = 1; // calculated int tap, cycleLength; // = 2^n-1 // changing variables int step, value; // tap bit mask for different n's (2 through 24) static final int[] tapTable = { 0x3, 0x3, 0x3, 0x5, 0x3, 0x3, 0x2d, 0x11, 0x9, 0x5, 0x107, 0x27, 0x1007, 0x3, 0x100b, 0x9, 0x81, 0x27, 0x9, 0x5, 0x3, 0x21, 0x87 }; *(int *n) { assertBetween(2, 24, n); cycleLength = (1 << n)-1; tap = tapTable[n-2]; value = start; } int next() { if (++step == cycleLength) step = 0; int tapped = (countBits(value & tap) & 1) ^ 1; ret value = (value >>> 1) | (tapped << (n-1)); } // for debugging/visualization int tapped() { ret value & tap; } int tappedBitCount() { ret countBits(tapped()); } long nextLong() { ret uintToLong(next()); } }
Began life as a copy of #1035196
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035200 |
Snippet name: | SimpleLSFR - linear feedback shift register (repeating pseudo-RNG with a cycle length of 2^n-1, using ints, works) |
Eternal ID of this version: | #1035200/4 |
Text MD5: | cb7509eb456f2f3f900ef99c4e6c7be8 |
Transpilation MD5: | c106765482b5037611d31e6e7b3b2818 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-04-15 01:32:10 |
Source code size: | 1198 bytes / 48 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 129 / 179 |
Version history: | 3 change(s) |
Referenced in: | [show references] |