zoukankan      html  css  js  c++  java
  • random

    随机数种子确定了随机序列的产生
    >>> import random
    >>> random.seed(10)#不加参数,默认为当前系统时间
    >>> random.random()#生成一个[0.0,1.0)之间的随机小数
    0.5714025946899135
    >>> random.seed(9)
    >>> random.random()
    0.46300735781502145

    扩展随机函数
    >>> random.randint(a,b)#生成一个[a,b]之间的整数
    random.randint(1,3)
    3
    >>> random.randrange(m,n,p)#[m,n)之间步长为p
    random.randrange(10,100,10)
    70

    random.getrandbits(k)#生成<=k位的整数
    >>> random.getrandbits(10)
    143
    random.uniform(a,b)#生成一个[a,b]之间的随机小数
    >>> random.uniform(10,20)
    16.838507142281692
    >>> s = '1 2 3 6'
    >>> import random
    >>> random.choice(s)#在序列中随机选择一个
    '1'
    >>> s = [1,2,3,4,5,6,]
    >>> import random
    >>> random.shuffle(s)#将序列元素重新排列,
    >>> print(s)
    [2, 3, 6, 4, 5, 1]

    #利用random 计算圆周率
    from random import random
    from time import perf_counter
    darts = 1000*1000
    hits = 0.0
    start = perf_counter()
    for i in range(1,darts+1):
        x,y = random(),random()#[0,1.0)
        dis = pow(x**2+y**2,0.5)
        if dis <=1.0:
            hits+=1
    pi =4*(hits/darts)
    print("圆周率为 :{}".format(pi))
  • 相关阅读:
    update结合查询更新
    查表字段名,注释
    微信access_token
    Oracle中的dual伪表
    Oracle中的null
    UIView九宫格
    UIWebView使用
    sql触发器Tigger
    重写init方法
    OC内存管理示例
  • 原文地址:https://www.cnblogs.com/tingtin/p/11658708.html
Copyright © 2011-2022 走看看