in javascript you can't seed Math.random, if you want something you can seed you have to implement a PRNG yourself

i needed a function that returned a "random" value in [0, 1), like Math.random does, but seedable. importantly, it wasn't for anything even slightly cryptographic or whatever. i had absolutely no requirements for the quality of the output beyond "seems vaguely random to a person"

so here's the simplest, and probably worst (but good enough) PRNG function i found online:

let seed = WHATEVER_NUMBER;
function random() {
const x = seed++/Math.PI;
return x - Math.floor(x);
}

@monorail also, reported a second time for using a function declaration

@wallhackio is function random() {} equivalent to like var random = () => {}; and that's why it's bad or

@monorail @wallhackio it’s actually more like var random = function random() { } (function expressions can take an optional name that will appear in stack traces)

Follow

@monorail @wallhackio also there's the whole thing with this diffuring between arrow functions and function functions

@aescling @monorail yes all of this is valid but polluting the global namespace is the most important code smell

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.