Last active
August 20, 2024 11:03
-
-
Save kjlubick/6c35ba0dfb80b3df8ffa666fb2ec76ca to your computer and use it in GitHub Desktop.
Pyramid Dice Simulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright 2021 Kaylee 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