Skip to content

Instantly share code, notes, and snippets.

@nekonenene
Last active May 8, 2020 14:33
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 nekonenene/3194fbb5217643635903c52be258c68f to your computer and use it in GitHub Desktop.
Save nekonenene/3194fbb5217643635903c52be258c68f to your computer and use it in GitHub Desktop.
文字種類PのN文字をT回発行した時に重複する確率を 0.1% 未満にしたかった
package main
import (
"fmt"
"math"
)
// かぶらない可能性
// charPattern: 文字種, n: 文字数, tryCount: 試行回数
func notDuplicate(charPattern uint64, n uint64, tryCount uint64) float64 {
bo := uint64(math.Pow(float64(charPattern), float64(n))) // 分母
// fmt.Printf("全てのパターン: %d通り\n", bo)
if tryCount > bo {
return 0
}
num := bo - tryCount + 1
answer := 1.0
for {
answer *= (float64(num) / float64(bo))
num += 1
if num > bo {
break
}
}
return answer
}
func main() {
charPattern := uint64(64)
n := uint64(8)
tryCount := uint64(10000)
ndRate := notDuplicate(charPattern, n, tryCount)
duplicatePercent := (1.0 - ndRate) * 100
fmt.Printf("文字種が%dの%d文字を%d回発行して重複する確率: %v%%\n", charPattern, n, tryCount, duplicatePercent)
}
def main()
n = 8
bo = 64 ** n
t = 100000
return 100 if t > bo
start_num = bo - t + 1
answer = 1.0
(start_num..bo).to_a.each do |num|
answer = answer * (num.to_f / bo.to_f)
end
(1.0 - answer) * 100
end
puts main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment