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:\4software\python2.7\lib\random.py", line 323, in sample
  raise ValueError("sample larger than population")
  ValueError: sample larger than population

转载于:https://www.cnblogs.com/ddpeng/p/9759379.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值