zoukankan      html  css  js  c++  java
  • python->random模块用法

    random模块是不能直接访问的,需要引入该模块后,通过random的静态对象来调用该方法:

    • 模块导入:import random
    • random常用方法:random.random(),random.choice(self,seq),random.randint(self,a,b),random.sample(population, k)
    • >>> help(random.random)    #该帮助信息说明该方法可直接调用并返回0,1之间的随机数
      Help on built-in function random:

      random(...)
      random() -> x in the interval [0, 1).

      示例1:0-1随机生成浮点数 

      >>> random.random()
      0.3813265074990517
      >>> random.random()
      0.05383742352643661

      示例2:0-1随机生成保留2位小数的浮点数

      >>> round(random.random(),2)
      0.04
      >>> round(random.random(),2)
      0.69

    • >>> help(random.choice) #从一个序列中随机选取一个值并返回,该序列为元组、列表或者字符串

      Help on method choice in module random:

      choice(self, seq) method of random.Random instance
      Choose a random element from a non-empty sequence.

      示例1:字符串序列

      >>> seq = 'abcdefg'
      >>> random.choice(seq)
      'a'
      >>> random.choice(seq)
      'f'

      示例2:元组序列

      >>> seq = (1,2,3,4,5,6)
      >>> random.choice(seq)
      3
      >>> random.choice(seq)
      2

      示例3:列表序列

      >>> seq = [1,2,3,4,5,6]
      >>> random.choice(seq)
      5
      >>> random.choice(seq)
      6

    • >>> help(random.randint)   #从a-b区间中选取一个随机数并返回
      Help on method randint in module random:

      randint(self, a, b) method of random.Random instance
      Return random integer in range [a, b], including both end points

      示例:

      >>> random.randint(1,20)
      9
      >>> random.randint(1,20)
      15

    •  从序列population中选取k个唯一不重复的值,且k的值不能大于population的长度

      >>> help(random.sample)    
      Help on method sample in module random:

      sample(self, population, k) method of random.Random instance
      Chooses k unique random elements from a population sequence.

      Returns a new list containing elements from the population while
      leaving the original population unchanged. The resulting list is
      in selection order so that all sub-slices will also be valid random
      samples. This allows raffle winners (the sample) to be partitioned
      into grand prize and second place winners (the subslices).

      Members of the population need not be hashable or unique. If the
      population contains repeats, then each occurrence is a possible
      selection in the sample.

      To choose a sample in a range of integers, use xrange as an argument.
      This is especially fast and space efficient for sampling from a
      large population: sample(xrange(10000000), 60)

    • 示例:

      >>> random.sample(xrange(1000), 10)   #从0-1000中随机挑选10个唯一的值。
      [905, 561, 89, 287, 575, 131, 711, 416, 678, 650]

      >>> random.sample(xrange(10), 11)  #选取的个数大于总序列的长度时会报错,提示ValueError
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "E:4softwarepython2.7lib andom.py", line 323, in sample
      raise ValueError("sample larger than population")
      ValueError: sample larger than population

  • 相关阅读:
    MFC 记录 CreateProcess启动外部游戏主程序
    MFC 记录 CListCtrl 学习使用
    MS SQL自定义字符串拆分函数的惨痛经历
    C#路径/文件/目录/I/O常见操作汇总
    2012年开发者该做的11件事
    取出AD中一個組的所有成員信息(C#實現功能配合EXT做的界面)
    代码注释规范
    基于工作实际需求的Ext.Net和C#搭配应用之一 取出网域(AD)中所有计算机名及位置描述等信息
    2012,我的C#全能Excel操作(无需Office,不使用XML)
    一個文件文件和路徑的類
  • 原文地址:https://www.cnblogs.com/ddpeng/p/9759379.html
Copyright © 2011-2022 走看看