Hacker News new | ask | show | jobs
by nbevans 3565 days ago
Math.random doesn't necessarily have side affects. If it's storing a seed value, and incrementing that each time; then yes it stores mutable state which is a side affect because future invocations of that function will then depend upon that pre-existing state.

If however Math.random is passively listening for background radiation through some sensor perhaps then it doesn't really have side affects does it? Nor does it have mutable state, for that matter.

But it would still be impure according the true definition of what is a pure function, I believe.

3 comments

I thought it was a bit pedantic as well, at first. But looking at Haskell, I can see why the author made this point. Haskell functions won't accept random values as input, since they're monadic values.
Are there two identical inputs to Math.random that yield different results? If the answer to that question is yes, the function isn't pure.
Replace Math.random with this, then:

    ((j) => () => j++)(0)