class ProbabilityStatistics:

@staticmethod
def simulation_of_probability(v, ratio=10000):
assert v >= 0 and v <= 1
_v = int(v * ratio)
complement = ratio - _v
l = [0 for _ in range(_v)] + [1 for _ in range(complement)]
import random
# TODO numpy scipy
return random.choice(l) == 0

if __name__ == '__main__':
vs = (0.1, 0.5, 0.5123, 0.9)
for v in vs:
for times in (100, 1000, 10000, 100000):
ok = 0
for i in range(times):
b = ProbabilityStatistics.simulation_of_probability(v)
if b:
ok += 1
print('got-times,run-times,p,target', ok, times, ok / times, v)

got-times,run-times,p,target 14 100 0.14 0.1

got-times,run-times,p,target 83 1000 0.083 0.1

got-times,run-times,p,target 957 10000 0.0957 0.1

got-times,run-times,p,target 10171 100000 0.10171 0.1

got-times,run-times,p,target 42 100 0.42 0.5

got-times,run-times,p,target 524 1000 0.524 0.5

got-times,run-times,p,target 4940 10000 0.494 0.5

got-times,run-times,p,target 50071 100000 0.50071 0.5

got-times,run-times,p,target 55 100 0.55 0.5123

got-times,run-times,p,target 475 1000 0.475 0.5123

got-times,run-times,p,target 5205 10000 0.5205 0.5123

got-times,run-times,p,target 51188 100000 0.51188 0.5123

got-times,run-times,p,target 92 100 0.92 0.9

got-times,run-times,p,target 902 1000 0.902 0.9

got-times,run-times,p,target 9035 10000 0.9035 0.9

got-times,run-times,p,target 90093 100000 0.90093 0.9


class ProbabilityStatistics:

@staticmethod
def simulation_of_probability(v, ratio=10000):
assert v >= 0 and v <= 1
_v = int(v * ratio)
complement = ratio - _v
l = [0 for _ in range(_v)] + [1 for _ in range(complement)]
import random
# TODO numpy scipy
return random.choice(l) == 0

if __name__ == '__main__':
vs = (0.1, 0.5, 0.5123, 0.9)
for v in vs:
for times in (100, 1000, 10000, 100000):
ok = 0
for i in range(times):
b = ProbabilityStatistics.simulation_of_probability(v)
if b:
ok += 1
print('got-times,run-times,p,target', ok, times, ok / times, v)

ProbabilityStatistics的更多相关文章

  1. 【javascript】:Highcharts实战

    PS: Highcharts是一款前端图标设计框架,非常绚. 前端JS: var probabilityStatisticsData; var yearTool; var CoordinateX = ...

随机推荐

  1. 面试官让你讲讲Linux内核的竞争与并发,你该如何回答?

    @ 目录 内核中的并发和竞争简介 原子操作 原子操作简介 整型原子操作函数 位原子操作函数 原子操作例程 自旋锁 自旋锁简介 自旋锁操作函数 自旋锁例程 读写自旋锁 读写锁例程 顺序锁 顺序锁操作函数 ...

  2. oracle 19c dataguard silent install (oracle 19c dataguard 静默安装)

    环境说明 1.关闭透明大页 RHEL  6: # cat /sys/kernel/mm/redhat_transparent_hugepage/enabled [oracle@rhel 6 ~]$ c ...

  3. 使用pdf2htmlEX将pdf文件转为html

    https://github.com/coolwanglu/pdf2htmlEX 参考github文档,转换出来的的效果貌似很好,可以参考OFFICE 文档转换为html在线预览. pdf2swf 和 ...

  4. %@ taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%报错

    用eclipse写jsp代码时发现下面两行代码报错:<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix=&qu ...

  5. eclips快捷键

    所谓"工欲善其事必先利其器",程序写多了,对于快捷键总有些特别的偏爱.在众多编辑器中,Eclipse算是用的比较多,也是最熟的. 最常用(也是最爱的:)) Ctrl+' :  自动 ...

  6. [leetcode]39combinationsum回溯法找几个数的和为目标值

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Given a set of can ...

  7. 什么是Solon?

    Solon是参考Spring boot 和 Javalin 而设计.吸取了两者的的优点,避开了很多繁重的设计,使其支持http, websocket, socket 三种通讯信号接入.Solon 2M ...

  8. Vs2017编译器提示:不能将“const char *”类型的值分配到“char *”类型的实体

    在项目属性中将语言符合模式改成否即可

  9. ZooKeeper集群“脑裂”

    ZooKeeper 集群节点为什么要部署成奇数ZooKeeper 容错指的是:当宕掉几个ZooKeeper节点服务器之后,剩下的个数必须大于宕掉的个数,也就是剩下的节点服务数必须大于n/2,这样Zoo ...

  10. haproxy 支持 websocket

    haproxy支持websocket feat 通过嗅探http请求中的Connection: Upgrade Upgrade: websocket头部,来自动识别是否是websocket连接,识别成 ...