Skip to content

Instantly share code, notes, and snippets.

@plugnburn
Forked from 140bytes/LICENSE.txt
Last active January 8, 2022 19:40
Show Gist options
  • Save plugnburn/6364166 to your computer and use it in GitHub Desktop.
Save plugnburn/6364166 to your computer and use it in GitHub Desktop.
Linear Feedback Shift Register implementation

LFSR-golf

An implementation of Linear Feedback Shift Register based pseudo-random sequence generator in 120 bytes of JavaScript.

Usage

LFSR(length,seed)

e.g.

LFSR(865,1567)

will generate a sequence 865 bytes long from the seed 1567 and return a raw JS string containing it.

function(l,f,s,r){for(r=s="",l*=8;l--;)l&7?s=s<<1|(f=f>>1^(f&1&&0x80000057))&1:(r+=String.fromCharCode(s),s=0);return r}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 plugnburn
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "LFSR",
"description": "Linear Feedback Shift Register PRNG",
"keywords": [
"LFSR",
"random",
"generator",
"number",
"sequence"
]
}
@atk
Copy link

atk commented Aug 28, 2013

You can use coercion to initialize s and save 2 bytes:

function(l,f,s,r){for(r=s="",l*=8;l--;)l&7?s=s<<1|(f=f>>1^(f&1&&0x80000057))&1:(r+=String.fromCharCode(s),s=0);return r}

@plugnburn
Copy link
Author

Well, thanks.

@atk
Copy link

atk commented Aug 28, 2013

You're welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment