zoukankan      html  css  js  c++  java
  • Python随机数与随机字符串详解

    随机整数:>>>importrandom>>>random randint(0,99)21随机选取0到100间的偶数:>>>importrandom>>>random randrange(0,101,2)42随机浮点数

    随机整数:

    1
    2
    3
    >>> import random
    >>> random.randint(0,99)
    21

    随机选取0到100间的偶数:

    1
    2
    3
    >>> import random
    >>> random.randrange(01012)
    42

    随机浮点数:

    1
    2
    3
    4
    5
    >>> import random
    >>> random.random() 
    0.85415370477785668
    >>> random.uniform(110)
    5.4221167969800881

    随机字符:

    1
    2
    3
    >>> import random
    >>> random.choice('abcdefg&#%^*f')
    'd'

    多个字符中选取特定数量的字符:

    1
    2
    3
    >>> import random
    random.sample('abcdefghij',3
    ['a''d''b']

    多个字符中选取特定数量的字符组成新字符串:

    1
    2
    3
    4
    5
    >>> import random
    >>> import string
    >>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
    eplace(" ","")
    'fih'

    随机选取字符串:

    1
    2
    3
    >>> import random
    >>> random.choice ( ['apple''pear''peach''orange''lemon'] )
    'lemon'

    洗牌:

    1
    2
    3
    4
    5
    >>> import random
    >>> items = [123456]
    >>> random.shuffle(items)
    >>> items
    [325641]

    random的函数还有很多,此处不一一列举,

  • 相关阅读:
    chart制作流程
    grep通过下面參数控制上下文
    PV、PVC、StorageClass讲解
    CentOS 7 安装NFS
    查看K8S的kubectl命令行缩写
    rpm&yum使用
    查看证书有效期
    kubectl-debug
    Docker镜像空间清理
    Matlab画图,坐标轴范围设置和间隔设置
  • 原文地址:https://www.cnblogs.com/chenjingyi/p/5740451.html
Copyright © 2011-2022 走看看