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

熱線電話:13121318867

登錄
首頁精彩閱讀Python隨機生成均勻分布在單位圓內(nèi)的點代碼示例
Python隨機生成均勻分布在單位圓內(nèi)的點代碼示例
2018-01-22
收藏

Python隨機生成均勻分布在單位圓內(nèi)的點代碼示例

Python有一隨機函數(shù)可以產(chǎn)生[0,1)區(qū)間內(nèi)的隨機數(shù),但是如果我們想生成隨機分布在單位圓上的,那么我們可以首先生成隨機分布在單位圓邊上的點,然后隨機調(diào)整每個點距離原點的距離,但是我們發(fā)現(xiàn)這個距離不是均勻分布于[0,1]的,而是與扇形的面積相關(guān)的

我們使用另外的隨機函數(shù)生成從[0,1)的隨機數(shù)r,我們發(fā)現(xiàn)r<s0的概率為s0,顯而易見,如果r為0,那么對應(yīng)的距離應(yīng)該為0,如果是1,對應(yīng)的距離自然也應(yīng)該是1,假設(shè)我們產(chǎn)生了m個隨機數(shù),那么小于s0的隨機數(shù)應(yīng)該為s0*m左右,而且這些應(yīng)該對應(yīng)于扇形面積的s0倍處即圖2的小扇形區(qū)域,落在這一區(qū)域的點應(yīng)該為s0*m,此時扇形邊長為s0^0.5,因此s0對應(yīng)的距離應(yīng)該為s0^0.5,因此我們得到的映射函數(shù)為y=x^0.5(圖1)

圖1

圖2

因此我們對于每個頂點的邊長便是產(chǎn)生隨機數(shù)的算術(shù)平方根的大小

附代碼如下:

# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
 
if __name__=='__main__':
  samples_num = 800
  t = np.random.random(size=samples_num) * 2 * np.pi - np.pi
  x = np.cos(t)
  y = np.sin(t)
  i_set = np.arange(0,samples_num,1)
  for i in i_set:
    len = np.sqrt(np.random.random())
    x[i] = x[i] * len
    y[i] = y[i] * len
  plt.figure(figsize=(10,10.1),dpi=125)
  plt.plot(x,y,'ro')
  _t = np.arange(0,7,0.1)
  _x = np.cos(_t)
  _y = np.sin(_t)
  plt.plot(_x,_y,'g-')
  plt.xlim(-1.1,1.1)
  plt.ylim(-1.1,1.1)
  plt.xlabel('x')
  plt.ylabel('y')
  plt.title('Random Scatter')
  plt.grid(True)
  plt.savefig('imag.png')

plt.show()


圖3

總結(jié)

以上就是本文關(guān)于Python隨機生成均勻分布在單位圓內(nèi)的點代碼示例的全部內(nèi)容,希望對大家有所幫助。



數(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 進(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ù)器是否宕機 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); }