Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.