Skip to content

Instantly share code, notes, and snippets.

@plugnburn

plugnburn/LICENSE.txt

Forked from 140bytes/LICENSE.txt
Last active Mar 22, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@atk 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

This comment has been minimized.

Copy link
Owner Author

@plugnburn plugnburn commented Aug 28, 2013

Well, thanks.

@atk

This comment has been minimized.

Copy link

@atk 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