python隨機數(shù)分布random測試
因為概率問題,所以需要測試一下python的隨機數(shù)分布。到底是平均(均勻)分布,還是正態(tài)(高斯)分布。
測試代碼如下:
#! /usr/bin/env python #coding=utf-8 # ================================= # Describe : 測試random隨機數(shù)分布 # 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] # 計數(shù)字典 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 # 計數(shù)字典 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()
測試結(jié)果:
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}
結(jié)果說明:平均(均勻)分布。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python random模塊制作簡易的四位數(shù)驗證碼
- Python常用模塊sys,os,time,random功能與用法實例分析
- python隨機數(shù)分布random均勻分布實例
- Python使用random模塊生成隨機數(shù)操作實例詳解
- Python隨機函數(shù)庫random的使用方法詳解
- 詳解Python利用random生成一個列表內(nèi)的隨機數(shù)
- Python3內(nèi)置模塊random隨機方法小結(jié)
- Python內(nèi)置random模塊生成隨機數(shù)的方法
- Python3.5內(nèi)置模塊之random模塊用法實例分析
- 詳解Python基礎(chǔ)random模塊隨機數(shù)的生成
- Python中的random.uniform()函數(shù)教程與實例解析
- 對Python random模塊打亂數(shù)組順序的實例講解
- Random 在 Python 中的使用方法
- python隨機模塊random使用方法詳解
相關(guān)文章
Python基于Serializer實現(xiàn)字段驗證及序列化
這篇文章主要介紹了Python基于Serializer實現(xiàn)字段驗證及序列化,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11Python使用sftp實現(xiàn)上傳和下載功能(實例代碼)
在Python中可以使用paramiko模塊中的sftp登陸遠程主機,實現(xiàn)上傳和下載功能。接下來通過本文給大家介紹Python使用sftp實現(xiàn)上傳和下載功能,需要的朋友參考下2017-03-03PyTorch的自適應(yīng)池化Adaptive Pooling實例
今天小編就為大家分享一篇PyTorch的自適應(yīng)池化Adaptive Pooling實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01使用Django框架中ORM系統(tǒng)實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)增刪改查
這篇文章主要介紹了使用Django的ORM實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)增刪改查方法,文中附含詳細示例代碼以及過程詳解,有需要的朋友可以借鑒參考下2021-09-09