Skip to content

Instantly share code, notes, and snippets.

@txus
Created May 4, 2016 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save txus/ea44239f0b2880766671d054be9dd4c8 to your computer and use it in GitHub Desktop.
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.
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