adding two more examples for preservation
This commit is contained in:
39
cmd/random-order/main.go
Normal file
39
cmd/random-order/main.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"hash/crc64"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
var (
|
||||
names = []string{
|
||||
"John",
|
||||
"Paul",
|
||||
"Peter",
|
||||
"Sam",
|
||||
"Susan",
|
||||
"Sarah",
|
||||
"Melony",
|
||||
}
|
||||
date = "2024-08-14"
|
||||
)
|
||||
|
||||
func removeIndex(s []string, i int) []string {
|
||||
ret := make([]string, 0)
|
||||
ret = append(ret, s[:i]...)
|
||||
return append(ret, s[i+1:]...)
|
||||
}
|
||||
|
||||
func main() {
|
||||
seed := crc64.Checksum([]byte(date), crc64.MakeTable(crc32.IEEE))
|
||||
r := rand.New(rand.NewSource((int64(seed))))
|
||||
|
||||
totalNames := len(names) - 1
|
||||
for i := 0; i <= totalNames; i++ {
|
||||
index := r.Intn(len(names))
|
||||
fmt.Printf("%s\n", names[index])
|
||||
names = removeIndex(names, index)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user