Created
November 4, 2016 12:49
-
-
Save enil/85176c86f642ef7adbeef3fb1383d2fc to your computer and use it in GitHub Desktop.
Pick N random values from a slice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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