1×
Become a member
to unlock all features
Autoplay

    Generate Randomness Using the State ADT

    javascriptJavaScript

    While the Math.random function that ships with JavaScript is super handy to have, it can make functional programmers cringe due to its, well randomness. We have no control over how the RNG is seeded, which removes all purity from the function and makes it difficult to test without mocking or implementing features like replays or time travel. We can take advantage of the nature of the State ADT by implementing a Linear Congruential Generator and keeping track of our seed in the state.