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. web基础知识,

    # web基础 网上冲浪 surfing the Internet weibo.com 域名,主机名,微博服务器的地址名 当用户在地址栏输入一个URL(uniform resource,locator ...

  2. JAVA中IO流详解

    IO流:数据传输是需要通道的,而IO流就是数据传输的通道. IO流可以形象的比喻为运送货物的传输带. IO流的分类: ①根据操作的数据类型的不同可以分为 :字节流与字符流. ②根据数据的流向分为:输入 ...

  3. 关闭layer

    function closeBox() { var index = parent.layer.getFrameIndex(window.name); //获取当前窗体索引 parent.layer.c ...

  4. 为了搞清楚类加载,竟然手撸JVM!

    作者:小傅哥 博客:https://bugstack.cn Github:https://github.com/fuzhengwei/CodeGuide/wiki 沉淀.分享.成长,让自己和他人都能有 ...

  5. sql语句查询,limit与order by 同时出现,应该order by在前面

    eg:select orderid,status,createtime from orders where appid = :appId and userid = :userId order by c ...

  6. 聊一聊这个总下载量3603w的xss库,是如何工作的?

    上篇文章这一次,彻底理解XSS攻击讲解了XSS攻击的类型和预防方式,本篇文章我们来看这个36039K的XSS-NPM库(你没有看错就是3603W次, 36039K次,36,039,651次,数据来自h ...

  7. 面试官:数据库自增ID用完了会怎么样?

    看到这个问题,我想起当初玩魔兽世界的时候,25H难度的脑残吼的血量已经超过了21亿,所以那时候副本的BOSS都设计成了转阶段.回血的模式,因为魔兽的血量是int型,不能超过2^32大小. 估计暴雪的设 ...

  8. 什么时候使用Get请求/POST请求?

    当请求无副作用时(如进行搜索),便可使用GET方法:当请求有副作用时(如添加数据行),则用POST方法. 一个比较实际的问题是:GET方法可能会产生很长的URL,或许会超过某些浏览器与服务器对URL长 ...

  9. 杭电1720---A+B Coming(技巧:使用%x)

    Problem Description Many classmates said to me that A+B is must needs. If you can't AC this problem, ...

  10. ASP.NET Core 3.1 中间件

    参考微软官方文档 : https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1 ...