• 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

  • 相关阅读:
    Java找N个数中最小的K个数,PriorityQueue和Arrays.sort()两种实现方法
    POJ 1661 Help Jimmy(C)动态规划
    LeetCode第8场双周赛(Java)
    Eclipse访问外部网站(比如:CSDN首页)
    LeetCode第151场周赛(Java)
    LeetCode第152场周赛(Java)
    Eclipse Block Selection(块选择)快捷键 Alt + Shift + A
    PAT(B) 1090 危险品装箱(Java)
    PAT(B) 1050 螺旋矩阵(Java:24分)
    PAT(B) 1045 快速排序(C)
  • 原文地址:https://www.cnblogs.com/ddpeng/p/9759379.html
Copyright © 2020-2023  润新知