Skip to content

Instantly share code, notes, and snippets.

@enil
Created November 4, 2016 12:49
Show Gist options
  • Save enil/85176c86f642ef7adbeef3fb1383d2fc to your computer and use it in GitHub Desktop.
Save enil/85176c86f642ef7adbeef3fb1383d2fc to your computer and use it in GitHub Desktop.
Pick N random values from a slice
func min(x, y int) int {
if x > y {
return y
} else {
return x
}
}
func Sample(source []int, count int) []int {
count = min(count, len(source))
target := make([]int, count)
for targetIndex, sourceIndex := range rand.Perm(len(source))[:count] {
target[targetIndex] = source[sourceIndex]
}
return target
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment