Skip to content

Instantly share code, notes, and snippets.

@fwessels
Last active December 26, 2018 21:42
Show Gist options
  • Save fwessels/dbbafd537c13ec8f88b360b3a0091ac0 to your computer and use it in GitHub Desktop.
Save fwessels/dbbafd537c13ec8f88b360b3a0091ac0 to your computer and use it in GitHub Desktop.
Create a list of random servers
package main
import (
"fmt"
"hash/crc32"
)
func randomServers(token string, count int) []int {
hTok := crc32.Checksum([]byte(token), crc32.IEEETable)
val := uint32(hTok)
// initialize array to select from
ints := make([]int, count)
for i := 0; i < count; i++ {
ints[i] = i + 1
}
result := make([]int, 0, count)
for i := count; i >= 2; i-- {
// determine position and add to result
m := val % uint32(i)
result = append(result, ints[m])
// remove item just appended to result
ints = append(ints[0:m], ints[m+1:len(ints)]...)
}
// append last item
return append(result, ints[0])
}
func main() {
fmt.Println(randomServers("minio", 16)[:12])
// Prints: [6 10 2 9 8 16 5 4 13 3 15 12]
fmt.Println(randomServers("rocks", 16)[:12])
// Prints:[5 11 15 10 1 3 8 2 12 14 4 16]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment