Logo

Programming-Idioms

History of Idiom 11 > diff from v40 to v41

Edit summary for version 41 by programming-idioms.org:
New Go implementation by user [programming-idioms.org]

Version 40

2016-04-18, 20:18:42

Version 41

2016-04-20, 20:23:02

Idiom #11 Pick a random element from a list

List x must be non-empty.

Idiom #11 Pick a random element from a list

List x must be non-empty.

Imports
import "math/rand"
Code
func pickT(x []T) T {
	return x[rand.Intn(len(list))]
}
Comments bubble
If you decide to implement pickT, you will have to write it separately for each desired type T.
Doc URL
https://golang.org/pkg/math/rand/#Intn
Demo URL
http://play.golang.org/p/RFt6HM-GQx