99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python生成隨機(jī)數(shù)組的方法小結(jié)
Python生成隨機(jī)數(shù)組的方法小結(jié)
2018-07-03
收藏

Python生成隨機(jī)數(shù)組的方法小結(jié)

本文實例講述了Python生成隨機(jī)數(shù)組的方法。分享給大家供大家參考,具體如下:
研究排序問題的時候常常需要生成隨機(jī)數(shù)組來驗證自己排序算法的正確性和性能,今天把Python生成隨機(jī)數(shù)組的方法稍作總結(jié),以備以后查看使用。
一、使用random模塊生成隨機(jī)數(shù)組
python的random模塊中有一些生成隨機(jī)數(shù)字的方法,例如random.randint, random.random, random.uniform, random.randrange,這些函數(shù)大同小異,均是在返回指定范圍內(nèi)的一個整數(shù)或浮點數(shù),下邊簡單解釋一下這幾個函數(shù)。
1、random.randint(low, hight) -> 返回一個位于[low,hight]之間的整數(shù)
該函數(shù)接受兩個參數(shù),這兩個參數(shù)必須是整數(shù)(或者小數(shù)位是0的浮點數(shù)),并且第一個參數(shù)必須不大于第二個參數(shù)    
>>> import random
>>> random.randint(1,10)
5
>>> random.randint(1.0, 10.0)
5

2、random.random() -> 不接受參數(shù),返回一個[0.0, 1.0)之間的浮點數(shù)    
>>> random.random()
0.9983625479554628

3、random.uniform(val1, val2) -> 接受兩個數(shù)字參數(shù),返回兩個數(shù)字區(qū)間的一個浮點數(shù),不要求val1小于等于val2    
>>> random.uniform(1,5.0)
2.917249424176132
>>> random.uniform(9.9, 2)
3.4288029275359024

*4、random.randrange(start, stop, step) -> 返回以start開始,stop結(jié)束,step為步長的列表中的隨機(jī)整數(shù),同樣,三個參數(shù)均為整數(shù)(或者小數(shù)位為0),若start大于stop時 ,setp必須為負(fù)數(shù).step不能是0.*    
>>> random.randrange(1, 100, 2) #返回[1,100]之間的奇數(shù)
95
>>> random.randrange(100, 1, -2) #返回[100,1]之間的偶數(shù)
46

運(yùn)行效果圖如下:

5、生成隨機(jī)數(shù)組
下邊我們用random.randint來生成一個隨機(jī)數(shù)組    
import random
def random_int_list(start, stop, length):
  start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))
  length = int(abs(length)) if length else 0
  random_list = []
  for i in range(length):
    random_list.append(random.randint(start, stop))
  return random_list

接下來我們就可以用這個函數(shù)來生成一個隨機(jī)的整數(shù)序列了    
>>> random_int_list(1,100,10)
[54, 13, 6, 89, 87, 39, 60, 2, 63, 61]
二、使用numpy.random模塊來生成隨機(jī)數(shù)組
1、np.random.rand 用于生成[0.0, 1.0)之間的隨機(jī)浮點數(shù), 當(dāng)沒有參數(shù)時,返回一個隨機(jī)浮點數(shù),當(dāng)有一個參數(shù)時,返回該參數(shù)長度大小的一維隨機(jī)浮點數(shù)數(shù)組,參數(shù)建議是整數(shù)型,因為未來版本的numpy可能不支持非整形參數(shù)。    
import numpy as np
>>> np.random.rand(10)
array([ 0.56911206, 0.99777291, 0.18943144, 0.19387287, 0.75090637,
    0.18692814, 0.69804514, 0.48808425, 0.79440667, 0.66959075])

當(dāng)然該函數(shù)還可以用于生成多維數(shù)組,這里不做詳述。

2、np.random.randn該函數(shù)返回一個樣本,具有標(biāo)準(zhǔn)正態(tài)分布。    
>>> np.random.randn(10)
array([-1.6765704 , 0.66361856, 0.04029481, 1.19965741, -0.57514593,
    -0.79603968, 1.52261545, -2.17401814, 0.86671727, -1.17945975])

3、np.random.randint(low[, high, size]) 返回隨機(jī)的整數(shù),位于半開區(qū)間 [low, high)。    
>>> np.random.randint(10,size=10)
array([4, 1, 4, 3, 8, 2, 8, 5, 8, 9])

4、random_integers(low[, high, size]) 返回隨機(jī)的整數(shù),位于閉區(qū)間 [low, high]。    
>>> np.random.random_integers(5)
4

5、np.random.shuffle(x) 類似洗牌,打亂順序;np.random.permutation(x)返回一個隨機(jī)排列    
>>> arr = np.arange(10)
>>> np.random.shuffle(arr)
>>> arr
[1 7 5 2 9 4 3 6 0 8]
>>>> np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }