Logo

Programming-Idioms

History of Idiom 15 > diff from v29 to v30

Edit summary for version 30 by :
New Csharp implementation by user [zehberk]

Version 29

2016-02-16, 17:56:27

Version 30

2016-02-18, 16:57:57

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.

Imports
import "math/rand"
Imports
import "math/rand"
Code
func pick(a,b int) int {
	return a + rand.Intn(b-a+1)
}
Code
func pick(a,b int) int {
	return a + rand.Intn(b-a+1)
}
Comments bubble
(b-a+1) is needed to have upper bound b included.
Comments bubble
(b-a+1) is needed to have upper bound b included.
Doc URL
https://golang.org/pkg/math/rand/#Intn
Doc URL
https://golang.org/pkg/math/rand/#Intn
Demo URL
http://play.golang.org/p/Tk1Gl2sxv-
Demo URL
http://play.golang.org/p/Tk1Gl2sxv-