zoukankan      html  css  js  c++  java
  • python中random的基本用法

    那么怎么使 a 随机生成一个数值呢,来研究一下random的部分程序;python中random模块的几个函数可以随机生成数值,下面咱们看一下random的几个函数的使用方法。

    random()是不能直接访问的,需要导入random模块通过random静态调用该方法。
    随机浮点数
    random.uniform() 
    用于生成指定范围内的浮点数,两个参数,一个是上限,一下是下限

    1 import random
    2 a = random.uniform(2,10)
    3 print a
    运行之后 a 就会随机选取2 —10之间的随机浮点数
     
    random.random()
    用于选取随机浮点数
     
    随机整数
    random.randint()
    用于选取指定范围内的整数;
    1 a  = random.randint(10,80)
    2 print a
    运行之后 a 就会随机选取10—80之间的随机整数
    random.randrange(0,100,2)
    用于随机选取0—100之间的随机偶数
     
    随机字符
    random.choice()
    用于随机选取指定字符串中的字符
    random.sample('qwertyuiop',3)
    用于选取字符串中随机3个字符
     
    random.choice()还可以随机选取字符串,在使用过程中要讲字符串加入到列表中使用
     
    1 a = random.choice(['one','two','three','four'])
    2 print a

    运行之后会选取one two three four中任意一个字符串

     

  • 相关阅读:
    1163
    1162
    1154
    Qt532_QWebView做成DLL供VC/Delphi使用_Bug
    Qt532__std::numeric_limits<qint64>::min();
    Qt532_WebKit_SSL问题
    Qt532_自定义QWebView_01
    HTML解析
    Qt5_pro_02
    Qt5需要的_libstdc++6_4.7.2-5_???.deb
  • 原文地址:https://www.cnblogs.com/llbb/p/11598547.html
Copyright © 2011-2022 走看看