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

熱線電話:13121318867

登錄
首頁精彩閱讀Python數(shù)學(xué)與隨機數(shù) (math包,random包)
Python數(shù)學(xué)與隨機數(shù) (math包,random包)
2017-04-16
收藏

Python數(shù)學(xué)與隨機數(shù) (math包,random包)

我們已經(jīng)在Python運算中看到Python最基本的數(shù)學(xué)運算功能。此外,math包補充了更多的函數(shù)。當(dāng)然,如果想要更加高級的數(shù)學(xué)功能,可以考慮選擇標(biāo)準(zhǔn)庫之外的numpy和scipy項目,它們不但支持?jǐn)?shù)組和矩陣運算,還有豐富的數(shù)學(xué)和物理方程可供使用。

此外,random包可以用來生成隨機數(shù)。隨機數(shù)不僅可以用于數(shù)學(xué)用途,還經(jīng)常被嵌入到算法中,用以提高算法效率,并提高程序的安全性。
math包
math包主要處理數(shù)學(xué)相關(guān)的運算。math包定義了兩個常數(shù):
math.e   # 自然常數(shù)e
math.pi  # 圓周率pi
此外,math包還有各種運算函數(shù) (下面函數(shù)的功能可以參考數(shù)學(xué)手冊):
math.ceil(x)       # 對x向上取整,比如x=1.2,返回2
math.floor(x)      # 對x向下取整,比如x=1.2,返回1
math.pow(x,y)      # 指數(shù)運算,得到x的y次方
math.log(x)        # 對數(shù),默認(rèn)基底為e??梢允褂胋ase參數(shù),來改變對數(shù)的基地。比如math.log(100,base=10)
math.sqrt(x)       # 平方根
三角函數(shù): math.sin(x), math.cos(x), math.tan(x), math.asin(x), math.acos(x), math.atan(x)
這些函數(shù)都接收一個弧度(radian)為單位的x作為參數(shù)。
角度和弧度互換: math.degrees(x), math.radians(x)
雙曲函數(shù): math.sinh(x), math.cosh(x), math.tanh(x), math.asinh(x), math.acosh(x), math.atanh(x)
特殊函數(shù): math.erf(x), math.gamma(x)
random包
如果你已經(jīng)了解偽隨機數(shù)(psudo-random number)的原理,那么你可以使用如下:
random.seed(x)
來改變隨機數(shù)生成器的種子seed。如果你不了解其原理,你不必特別去設(shè)定seed,Python會幫你選擇seed。
1) 隨機挑選和排序
random.choice(seq)   # 從序列的元素中隨機挑選一個元素,比如random.choice(range(10)),從0到9中隨機挑選一個整數(shù)。
random.sample(seq,k) # 從序列中隨機挑選k個元素
random.shuffle(seq)  # 將序列的所有元素隨機排序
2)隨機生成實數(shù)
下面生成的實數(shù)符合均勻分布(uniform distribution),意味著某個范圍內(nèi)的每個數(shù)字出現(xiàn)的概率相等:
random.random()          # 隨機生成下一個實數(shù),它在[0,1)范圍內(nèi)。
random.uniform(a,b)      # 隨機生成下一個實數(shù),它在[a,b]范圍內(nèi)。
下面生成的實數(shù)符合其它的分布 (你可以參考一些統(tǒng)計方面的書籍來了解這些分布):
random.gauss(mu,sigma)    # 隨機生成符合高斯分布的隨機數(shù),mu,sigma為高斯分布的兩個參數(shù)。
random.expovariate(lambd) # 隨機生成符合指數(shù)分布的隨機數(shù),lambd為指數(shù)分布的參數(shù)。
假設(shè)我們有一群人參加舞蹈比賽,為了公平起見,我們要隨機排列他們的出場順序。我們下面利用random包實現(xiàn):
import random數(shù)據(jù)分析師培訓(xùn)
all_people = ['Tom', 'Vivian', 'Paul', 'Liya', 'Manu', 'Daniel', 'Shawn']
random.shuffle(all_people)
for i,name in enumerate(all_people):
    print(i,':'+name)
練習(xí)
設(shè)計下面兩種彩票號碼生成器:
1. 從1到22中隨機抽取5個整數(shù) (這5個數(shù)字不重復(fù))
2. 隨機產(chǎn)生一個8位數(shù)字,每位數(shù)字都可以是1到6中的任意一個整數(shù)。
總結(jié)
math.floor(), math.sqrt(), math.sin(), math.degrees()
random.random(), random.choice(), random.shuffle()

數(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(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(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ù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 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); }