stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

int.random behaviour

Open schurhammer opened this issue 7 months ago • 3 comments

pub fn random(min: Int, max: Int) -> Int
  1. "min" and "max" can be used interchangeably, which is confusing to me. e.g. random(-1, 1) is the same as random(1, -1)
  2. the maximum value is excluded from output (i.e. not the max but the actually higher value)

I'm not sure if this behaviour is intended, but at least it needs to be documented.

schurhammer avatar Nov 27 '23 22:11 schurhammer

I did implement the first verion and I think someone changed it later.

"min" and "max" can be used interchangeably, which is confusing to me. e.g. random(-1, 1) is the same as random(1, -1)

This was in the first version and might be gone because people complained about performance instead of rolling their own. AFAIR.

the maximum value is excluded from output (i.e. not the max but the actually higher value)

This is behaviour in the Erlang implementation and the JS implementation says it should be the case but it isn't so there is a small check in the JS implementation, or at least that was the case.

inoas avatar Nov 28 '23 05:11 inoas

I think that #486 is addressing this issue; you can have a look at the documentation they added, I think it does a good job at explaining the behaviour of the random function Would this solve the issue, @schurhammer?

giacomocavalieri avatar Nov 28 '23 20:11 giacomocavalieri

Sorry I've not kept on top of this item of work. I'm unsure what is best here, and whether we should continue to do these checks for which of the two numbers is largest.

lpil avatar Nov 29 '23 11:11 lpil