adding two more examples for preservation

This commit is contained in:
2024-09-15 09:09:07 -05:00
parent 2c6aed01ae
commit 4d00710d32
2 changed files with 89 additions and 0 deletions

39
cmd/random-order/main.go Normal file
View 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)
}
}