zoukankan      html  css  js  c++  java
  • python 笔记 random

    记录Random的用法

    主要参考:python-3.7.0-docs-html/python-3.7.0-docs-html/library/random.html?highlight=random#module-random

    https://www.cnblogs.com/xybaby/p/8280936.html    (shuffle很详细)

    9.6.3. Functions for sequences

    random.choice(seq)

    Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

    random.choices(populationweights=None*cum_weights=Nonek=1)

    Return a k sized list of elements chosen from the population with replacement. If the population is empty, raises IndexError.

    If a weights sequence is specified, selections are made according to the relative weights. Alternatively, if a cum_weights sequence is given, the selections are made according to the cumulative weights (perhaps computed using itertools.accumulate()). For example, the relative weights [10, 5, 30, 5] are equivalent to the cumulative weights [10, 15, 45, 50]. Internally, the relative weights are converted to cumulative weights before making selections, so supplying the cumulative weights saves work.

    If neither weights nor cum_weights are specified, selections are made with equal probability. If a weights sequence is supplied, it must be the same length as the population sequence. It is a TypeError to specify both weights and cum_weights.

    The weights or cum_weights can use any numeric type that interoperates with the float values returned by random() (that includes integers, floats, and fractions but excludes decimals).

    New in version 3.6.

    random.shuffle(x[, random])

    Shuffle the sequence x in place.

    The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random().

    To shuffle an immutable sequence and return a new shuffled list, use sample(x, k=len(x)) instead.

    Note that even for small len(x), the total number of permutations of x can quickly grow larger than the period of most random number generators. This implies that most permutations of a long sequence can never be generated. For example, a sequence of length 2080 is the largest that can fit within the period of the Mersenne Twister random number generator.

    random.sample(populationk)

    Return a k length list of unique elements chosen from the population sequence or set. Used for randomsampling without replacement.

    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 from a range of integers, use a range() object as an argument. This is especially fast and space efficient for sampling from a large population: sample(range(10000000), k=60).

    If the sample size is larger than the population size, a ValueError is raised.

     

     1 import  random
     2 
     3 list_test = [1,2,3,4,5]
     4 print (random.choice(list_test))#3
     5 print (random.choices(list_test,weights=[100,0,0,0,100],k=5))#[5, 1, 1, 5, 1]
     6 print (random.choices(list_test,cum_weights=[100,100,100,100,200],k=5))#[5, 1, 1, 5, 1]
     7 print (random.shuffle(list_test))#None
     8 print (list_test)#[3, 2, 4, 5, 1]
     9 print(random.sample(list_test,3))#[3, 5, 2]
    10 same_list=[1,1,1,2]
    11 random.shuffle(same_list)
    12 print(same_list)#[2, 1, 1, 1]
    13 print(random.sample(same_list,3))#[1, 2, 1]

     针对9.6.3. Functions for sequences总结:

      1.random.sample(populationk)  权重和累加权重, For example, the relative weights [10, 5, 30, 5] are equivalent to the cumulative weights [10, 15, 45, 50]

     cumulative weights累加权重等价于relative weights的方式。 如上面代码5、6行是等价的,随机1和5的值概率分别为50%

      2.random.shuffle(x[, random]

        返回None   本身的x列表内容发生修改

        如果想保持传参只读,使用samper方式。针对const列表,shuffle不适用

      3.sampler方式

       如果列表内是相同值的,也会等价随机

       内部实现是进行随机索引和尾部交换,查看https://www.cnblogs.com/xybaby/p/8280936.html    (shuffle很详细) 

    后续。。。使用到再记录吧。。。

    改变自己
  • 相关阅读:
    解决Redis之MISCONF Redis is configured to save RDB snapshots
    lnmp一键安装redis以及php组件
    多版本PHP使用composer
    composer设置成阿里云镜像
    Yaf 简介
    Ubuntu配置php7.2开机启动
    Ubuntu查看开机日志
    ssh生成公钥
    Ubuntu系统如何分区
    初始化一个yaf项目
  • 原文地址:https://www.cnblogs.com/sun-shadow/p/9535469.html
Copyright © 2011-2022 走看看