python随机数分布random测试

作者:常城 时间:2022-01-05 00:41:17 

因为概率问题,所以需要测试一下python的随机数分布。到底是平均(均匀)分布,还是正态(高斯)分布。

测试代码如下:


#! /usr/bin/env python
#coding=utf-8

# =================================
# Describe :   测试random随机数分布
# D&P Author By:       常成功
# Create Date:      2017/10/07
# Modify Date:      2017/10/20
# (C) 2012-2017 All rights reserved
# =================================

import random
import time

def test_rnd():
 st_tm = time.time()
 j = 0
 num = 0
 the_list = [1, 2, 3, 4]
 # 计数字典
 temp_dic = {1: 0, 2: 0, 3: 0, 4: 0}
 while 1:
   x = random.choice(the_list)
   temp_dic[x] += 1
   j += 1
   # 跑一千万次
   if j >= 10000000:
     break
 ed_tm = time.time()
 print "Test random.choice()---------------------------:"
 print "loop num: ", j
 print "take time: ", ed_tm-st_tm
 print "temp_dic :", temp_dic

print "Test random.randint()---------------------------:"
 st_tm = time.time()
 j = 0
 num = 0
 # 计数字典
 temp_dic = {1: 0, 2: 0, 3: 0, 4: 0}
 while 1:
   x = random.randint(1, 4)
   temp_dic[x] += 1
   j += 1
   # 跑一千万次
   if j >= 10000000:
     break
 ed_tm = time.time()
 print "loop num: ", j
 print "take time: ", ed_tm-st_tm
 print "temp_dic :", temp_dic

if __name__ == '__main__':
 test_rnd()

测试结果:

Test random.choice()---------------------------:
loop num:  10000000
take time:  5.86599993706
temp_dic : {1: 2501333, 2: 2500117, 3: 2499406, 4: 2499144}
Test random.randint()---------------------------:
loop num:  10000000
take time:  12.493999958
temp_dic : {1: 2497732, 2: 2501411, 3: 2499372, 4: 2501485}

python随机数分布random测试

结果说明:平均(均匀)分布。

来源:https://blog.csdn.net/chenggong2dm/article/details/78350822

标签:python,随机数,random
0
投稿

猜你喜欢

  • 什么是用户体验优化(UEO)

    2008-08-27 19:49:00
  • Python:Scrapy框架中Item Pipeline组件使用详解

    2021-04-18 22:22:47
  • Django ORM 查询管理器源码解析

    2023-01-30 12:28:47
  • 一文搞懂Python中列表List和元组Tuple的使用

    2022-10-09 02:24:58
  • AspJpeg组件:介绍、注册、高级使用方法

    2010-01-25 12:42:00
  • javascript中类的创建和继承

    2008-05-08 12:07:00
  • Python基础教程之tcp socket编程详解及简单实例

    2021-04-18 12:04:29
  • Python实现对中文文本分段分句

    2022-09-16 18:16:50
  • Python随机数用法实例详解【基于random模块】

    2023-10-26 08:48:49
  • django云端留言板实例详解

    2023-12-13 08:17:59
  • Python中py文件转换成exe可执行文件的方法

    2022-09-30 02:01:40
  • 解析:轻松了解 MySQL中损坏的MyISAM表

    2009-02-23 17:30:00
  • CSS制作11种风格不同的特效文字

    2010-10-20 20:08:00
  • css中使用CLASS来设计网页布局

    2007-10-30 13:01:00
  • Python各种扩展名区别点整理

    2023-10-14 21:23:01
  • python selenium登录豆瓣网过程解析

    2021-12-15 09:52:49
  • 解析:以mysql_开始的未定义引用错误

    2008-12-19 17:58:00
  • Yahoo!网站性能最佳体验的34条黄金守则——图片、Coockie与移动应用

    2008-05-29 13:44:00
  • thinkphp5实用入门进阶知识点和各种常用功能代码汇总

    2023-05-25 02:48:34
  • python实现马耳可夫链算法实例分析

    2022-08-13 00:27:08
  • asp之家 网络编程 m.aspxhome.com