elm / random

Generate random values in Elm
https://package.elm-lang.org/packages/elm/random/latest/
BSD 3-Clause "New" or "Revised" License
46 stars 23 forks source link

`Random.float` lacks sanitization of input and emits invalid numbers when bounds are swapped #21

Open SiriusStarr opened 2 years ago

SiriusStarr commented 2 years ago

Random.int contains a sanitization step at: https://github.com/elm/random/blob/c1c9da4d861363cee1c93382d2687880279ed0dd/src/Random.elm#L81-L85

As such, Random.int 100 -100 produces values in the range [-100, 100]

Random.float contains no such step, and Random.float 100 -100 produces values in the range [100, 300] due to this, which assumes the bounds were passed in the correct order: https://github.com/elm/random/blob/c1c9da4d861363cee1c93382d2687880279ed0dd/src/Random.elm#L180-L182 There is no information in the documentation warning of this (otherwise silent) error, and it violates the principle of least surprise.

Either the same sanitization step should be added to Random.float or at the very least a prominent warning should be added to the documentation.