Logo

Programming-Idioms

History of Idiom 15 > diff from v30 to v31

Edit summary for version 31 by :
[Ruby] rand supports a range as argument

Version 30

2016-02-18, 16:57:57

Version 31

2016-04-07, 20:27:01

Idiom #15 Pick uniformly a random integer in [a..b]

Pick a random integer greater than or equals to a, inferior or equals to b. Precondition : a < b.

Idiom #15 Pick uniformly a random integer in [a..b]

Pick a random integer greater than or equals to a, inferior or equals to b. Precondition : a < b.

Code
def pick(a, b)
	return (a + rand * (b-a+1)).to_i
end
Code
rand(a..b)