tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
63
Task/Probabilistic-choice/Go/probabilistic-choice.go
Normal file
63
Task/Probabilistic-choice/Go/probabilistic-choice.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type mapping struct {
|
||||
item string
|
||||
pr float64
|
||||
}
|
||||
|
||||
func main() {
|
||||
// input mapping
|
||||
m := []mapping{
|
||||
{"aleph", 1 / 5.},
|
||||
{"beth", 1 / 6.},
|
||||
{"gimel", 1 / 7.},
|
||||
{"daleth", 1 / 8.},
|
||||
{"he", 1 / 9.},
|
||||
{"waw", 1 / 10.},
|
||||
{"zayin", 1 / 11.},
|
||||
{"heth", 1759 / 27720.}} // adjusted so that probabilities add to 1
|
||||
|
||||
// cumulative probability
|
||||
cpr := make([]float64, len(m)-1)
|
||||
var c float64
|
||||
for i := 0; i < len(m)-1; i++ {
|
||||
c += m[i].pr
|
||||
cpr[i] = c
|
||||
}
|
||||
|
||||
// generate
|
||||
const samples = 1e6
|
||||
occ := make([]int, len(m))
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
for i := 0; i < samples; i++ {
|
||||
r := rand.Float64()
|
||||
for j := 0; ; j++ {
|
||||
if r < cpr[j] {
|
||||
occ[j]++
|
||||
break
|
||||
}
|
||||
if j == len(cpr)-1 {
|
||||
occ[len(cpr)]++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// report
|
||||
fmt.Println(" Item Target Generated")
|
||||
var totalTarget, totalGenerated float64
|
||||
for i := 0; i < len(m); i++ {
|
||||
target := m[i].pr
|
||||
generated := float64(occ[i]) / samples
|
||||
fmt.Printf("%6s %8.6f %8.6f\n", m[i].item, target, generated)
|
||||
totalTarget += target
|
||||
totalGenerated += generated
|
||||
}
|
||||
fmt.Printf("Totals %8.6f %8.6f\n", totalTarget, totalGenerated)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue