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);
}
@wallhackio report me you won't
@monorail guess again fucker
@wallhackio do you like my javascript
@monorail const/let random = function() {} is also good
@wallhackio is function random() {}
equivalent to like var random = () => {};
and that's why it's bad or
@monorail this doesn't work in node.js for whatever reason, shittyRandom1 is not added to global scope
@wallhackio @monorail Global variables, as opposed to what?
You're declaring the function outside of any scope, so what else can it even be except global? Per-file scope like static
in C?
@vaporeon_ @monorail if my web page loaded two javascript files, (lets call them one.js and two.js)
and one.js defines a function
function globalFunc() {
console.log('I am global');
}
then two.js can use that function
@wallhackio @monorail Oh!
Functions are global by default in C, too, unless you declare the functions as static
wrong
@vaporeon_ @monorail huh i had no idea that c sucks too
re: wrong
@wallhackio @monorail Reported [not actually because I'm a coward] for slandering my favourite programming language
(Also, name a circumstance where you don't want functions to be global?)
re: wrong
@vaporeon_ @monorail in javascript functions can be reassigned
so lets say you work on a web site for your company that uses old-ass ES5 javascript because it was first made in 2009 and its for a government contractor who won't fund what is necessary for your team to change to a newer javascript workflow, so you're stuck with it
In one file, a coworker who no longer works at the company 10 years ago created a function called gorp
like so: function gorp() { /* whatever gorp does */}
Now, today you are working on a file that has a function called girp
and you reassign it something else. However, you make a typo and write gorp
instead. This does not throw at runtime because gorp
is global, and now you have bugs that are extremely difficult to decipher because you didn't notice a typo
@monorail @wallhackio it’s actually more like var random = function random() { }
(function expressions can take an optional name that will appear in stack traces)
@monorail @wallhackio also there's the whole thing with this
diffuring between arrow functions and function
functions
@monorail @wallhackio i won’t say “supposed” (function hoisting can be nice fur readability; openbsd c style is to do the equivalent and start with main()) but it’s very common
@wallhackio oh yeah are you supposed to do
const random () => {};
now or