Skip to content

Instantly share code, notes, and snippets.

@kjlubick
Last active July 16, 2022 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjlubick/6c35ba0dfb80b3df8ffa666fb2ec76ca to your computer and use it in GitHub Desktop.
Save kjlubick/6c35ba0dfb80b3df8ffa666fb2ec76ca to your computer and use it in GitHub Desktop.
Pyramid Dice Simulation
package main
import (
"fmt"
"math/rand"
"time"
)
func d6(rng *rand.Rand) int {
return rng.Intn(6) + 1
}
type rolls [2]int
func oneRound(rng *rand.Rand) int {
saw := map[rolls]bool{}
var key rolls
for i := 0; i < 21; i++ {
key[0], key[1] = d6(rng), d6(rng)
if key[1] > key[0] {
key[0], key[1] = key[1], key[0]
}
if saw[key] {
return i
}
saw[key] = true
}
return 21
}
func main() {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
var counts [22]int64
for i := 0; i < 1_500_000_000; i++ {
counts[oneRound(rng)]++
}
fmt.Printf("done %#v\n", counts)
}
Copyright 2021 Kevin Lubick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment