1. 9
    Generate Randomness Using the State ADT
    6m 29s

Generate Randomness Using the State ADT

Share this video with your friends

Send Tweet

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.