Created
May 4, 2016 14:29
-
-
Save txus/ea44239f0b2880766671d054be9dd4c8 to your computer and use it in GitHub Desktop.
Getting a random subset of size (N * percentage) of a slice of size N.
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 RandomSubset(containers []*docker.Container, percentage float64) []*docker.Container { | |
r := rand.New(rand.NewSource(time.Now().UnixNano())) | |
length := len(containers) | |
numberOfElementsWeWant := int64(percentage * float64(length)) | |
randomIndices := r.Perm(length)[0:numberOfElementsWeWant] | |
var selected []*docker.Container | |
for _, idx := range randomIndices { | |
selected = append(selected, containers[idx]) | |
} | |
return selected | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment