Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adamgfraser
Copy link
Contributor

Resolves #3146. These methods were added to scala.util.Random in 2.13 to generate numeric values within a specified range. Adding them to Random service maintains parity between our implementation of the standard services and the ones in the Scala standard library and should also provide an easier way for people to generate values in a range.

@adamgfraser adamgfraser requested a review from jdegoes March 15, 2020 21:58
@luis3m
Copy link
Contributor

luis3m commented Mar 16, 2020

@adamgfraser in the betweenWith definitions, isn't difference suppose to be always greater than zero given the condition above?

@adamgfraser
Copy link
Contributor Author

@luis3m We need it to deal with overflow. For example, say max is Int.MaxValue and min is -10. max is greater than min in that case, but if we do max - min we get -2147483639.

Conceptually the problem is that nextInt(n: Int) can only generate a space of integers up to Int.MaxValue. But the total space of integers is up to ~2X that. So if the space of integers that the caller is requesting is larger than that we need to fall back on the normal nextInt method and then retry until we get a value in range (which on average should only take two times).

@jdegoes jdegoes merged commit 3fe170b into zio:master Mar 16, 2020
@adamgfraser adamgfraser deleted the 3146 branch March 16, 2020 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Random range functions

3 participants