python中的random是啥意思-Python中random.sample和random.shuffle有什么区别

本文介绍了随机洗牌和从序列中选择不重复随机元素的算法实现。通过使用随机数生成器,在原地洗牌列表并返回None。此外,还提供了一个函数用于从指定集合中选择k个不重复的随机元素,并将这些元素存储在一个新的列表中。

洗牌的来源:

def shuffle(self, x, random=None, int=int):

"""x, random=random.random -> shuffle list x in place; return None.

Optional arg random is a 0-argument function returning a random

float in [0.0, 1.0); by default, the standard random.random.

"""

if random is None:

random = self.random

for i in reversed(xrange(1, len(x))):

# pick an element in x[:i+1] with which to exchange x[i]

j = int(random() * (i+1))

x[i], x[j] = x[j], x[i]

样本来源:

def sample(self, population, k):

"""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)

"""

# XXX Although the documentation says `population` is "a sequence",

# XXX attempts are made to cater to any iterable with a __len__

# XXX method. This has had mixed success. Examples from both

# XXX sides: sets work fine, and should become officially supported;

# XXX dicts are much harder, and have failed in various subtle

# XXX ways across attempts. Support for mapping types should probably

# XXX be dropped (and users should pass mapping.keys() or .values()

# XXX explicitly).

# Sampling without replacement entails tracking either potential

# selections (the pool) in a list or previous selections in a set.

# When the number of selections is small compared to the

# population, then tracking selections is efficient, requiring

# only a small set and an occasional reselection. For

# a larger number of selections, the pool tracking method is

# preferred since the list takes less space than the

# set and it doesn"t suffer from frequent reselections.

n = len(population)

if not 0 <= k <= n:

raise ValueError, "sample larger than population"

random = self.random

_int = int

result = [None] * k

setsize = 21 # size of a small set minus size of an empty list

if k > 5:

setsize += 4 ** _ceil(_log(k * 3, 4)) # table size for big sets

if n <= setsize or hasattr(population, "keys"):

# An n-length list is smaller than a k-length set, or this is a

# mapping type so the other algorithm wouldn"t work.

pool = list(population)

for i in xrange(k): # invariant: non-selected at [0,n-i)

j = _int(random() * (n-i))

result[i] = pool[j]

pool[j] = pool[n-i-1] # move non-selected item into vacancy

else:

try:

selected = set()

selected_add = selected.add

for i in xrange(k):

j = _int(random() * n)

while j in selected:

j = _int(random() * n)

selected_add(j)

result[i] = population[j]

except (TypeError, KeyError): # handle (at least) sets

if isinstance(population, list):

raise

return self.sample(tuple(population), k)

return result

如您所见,在两种情况下,随机化基本上都是通过int(random()* n)行完成的.因此,底层算法基本相同.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值