3 Star 25 Fork 17

Cliven/randomness

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
runs.go 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
Sun Yimin 提交于 2023-10-26 08:46 . 增加代码注释
// Copyright (c) 2021 Quan guanyu
// randomness is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package randomness
import (
"math"
)
// Runs 游程总数检测
func Runs(data []byte) *TestResult {
p, q := RunsTestBytes(data)
return &TestResult{Name: "游程总数检测", P: p, Q: q, Pass: p >= Alpha}
}
// RunsTestBytes 游程总数检测
func RunsTestBytes(data []byte) (float64, float64) {
return RunsTest(B2bitArr(data))
}
// RunsTest 游程总数检测
func RunsTest(bits []bool) (float64, float64) {
n := len(bits)
if n == 0 {
panic("please provide test bits")
}
var Pi float64 = 0
var V_obs int = 1
var P, Q float64 = 0, 0
// Step 1, 2
for i := 0; i < n-1; i++ {
if bits[i] != bits[i+1] {
V_obs++
}
if bits[i] {
Pi++
}
}
if bits[n-1] {
Pi++
}
Pi /= float64(n)
// Step 3, 第四、五步的除math.Sqrt(2),放到这里提前处理,减少math.Sqrt的调用。
V := (float64(V_obs) - 2.0*float64(n)*Pi*(1.0-Pi)) / (2.0 * math.Sqrt(float64(2*n)) * Pi * (1.0 - Pi))
// Step 4
P = math.Erfc(math.Abs(V))
// Step 5
Q = math.Erfc(V) / 2.0
return P, Q
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/Trisia/randomness.git
git@gitee.com:Trisia/randomness.git
Trisia
randomness
randomness
master

搜索帮助

Cb406eda 1850385 E526c682 1850385