Skip to content

Instantly share code, notes, and snippets.

@rikkix
Created April 25, 2021 08:46
Show Gist options
  • Save rikkix/ef640304d4a7fba6967e92364dfc831b to your computer and use it in GitHub Desktop.
Save rikkix/ef640304d4a7fba6967e92364dfc831b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"sort"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
max := 100
people := 5
times := 10000000
sum := make([]int, people)
for i := 0; i < times; i++ {
list := randomList(max, people-1)
sort.Ints(list)
result := make([]int, people)
for i := 0; i < people-2; i++ {
result[i+1] = list[i+1] - list[i]
}
result[0] = list[0]
result[people-1] = max - list[people-2]
for j := range result {
sum[j] += result[j]
}
}
for i := range sum {
fmt.Println(float64(sum[i]) / float64(max*times))
}
}
// randomList returns a random int list in range (0, max) with length of count
func randomList(max, count int) []int {
if count > max-1 {
return nil
}
list := make([]int, count)
for i := 0; i < count; i++ {
list[i] = rand.Intn(max-1) + 1
for j := 0; j < i; j++ {
if list[j] == list[i] {
i--
break
}
}
}
return list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment