[英]How do I create a list of random numbers without duplicates?
我尝试使用random.randint(0, 100)
,但有些数字是相同的。 是否有创建唯一随机数列表的方法/模块?
注意:以下代码基于答案,并在发布答案后添加。 这不是问题的一部分; 这是解决方案。
def getScores():
# open files to read and write
f1 = open("page.txt", "r");
p1 = open("pgRes.txt", "a");
gScores = [];
bScores = [];
yScores = [];
# run 50 tests of 40 random queries to implement "bootstrapping" method
for i in range(50):
# get 40 random queries from the 50
lines = random.sample(f1.readlines(), 40);
这将返回从 0 到 99 范围内选择的 10 个数字的列表,没有重复。
import random
random.sample(range(100), 10)
参考您的特定代码示例,您可能希望从文件中读取所有行一次,然后从内存中保存的列表中选择随机行。 例如:
all_lines = f1.readlines()
for i in range(50):
lines = random.sample(all_lines, 40)
这样,您只需要在循环之前从文件中实际读取一次。 这样做比回溯到文件的开头并为每次循环迭代再次调用f1.readlines()
效率更高。
您可以使用random模块中的shuffle函数,如下所示:
import random
my_list = list(xrange(1,100)) # list of integers from 1 to 99
# adjust this boundaries to fit your needs
random.shuffle(my_list)
print my_list # <- List of unique random numbers
请注意,shuffle 方法不会像预期的那样返回任何列表,它只会对通过引用传递的列表进行混洗。
您可以首先创建一个从a
到b
的数字列表,其中a
和b
分别是列表中的最小和最大数字,然后使用Fisher-Yates算法或使用 Python 的random.shuffle
方法对其进行洗牌。
O(1) 内存
O(k) 操作
这个问题可以用一个简单的线性同余生成器来解决。 这需要恒定的内存开销(8 个整数)和最多 2*(序列长度)计算。
所有其他解决方案都使用更多内存和更多计算! 如果你只需要几个随机序列,这种方法会便宜很多。 对于大小N
的范围,如果您想生成N
个或更多的唯一k
序列,我建议使用内置方法random.sample(range(N),k)
接受的解决方案,因为这 已在 python 中进行了优化为了速度。
# Return a randomized "range" using a Linear Congruential Generator
# to produce the number sequence. Parameters are the same as for
# python builtin "range".
# Memory -- storage for 8 integers, regardless of parameters.
# Compute -- at most 2*"maximum" steps required to generate sequence.
#
def random_range(start, stop=None, step=None):
import random, math
# Set a default values the same way "range" does.
if (stop == None): start, stop = 0, start
if (step == None): step = 1
# Use a mapping to convert a standard range into the desired range.
mapping = lambda i: (i*step) + start
# Compute the number of numbers in this range.
maximum = (stop - start) // step
# Seed range with a random integer.
value = random.randint(0,maximum)
#
# Construct an offset, multiplier, and modulus for a linear
# congruential generator. These generators are cyclic and
# non-repeating when they maintain the properties:
#
# 1) "modulus" and "offset" are relatively prime.
# 2) ["multiplier" - 1] is divisible by all prime factors of "modulus".
# 3) ["multiplier" - 1] is divisible by 4 if "modulus" is divisible by 4.
#
offset = random.randint(0,maximum) * 2 + 1 # Pick a random odd-valued offset.
multiplier = 4*(maximum//4) + 1 # Pick a multiplier 1 greater than a multiple of 4.
modulus = int(2**math.ceil(math.log2(maximum))) # Pick a modulus just big enough to generate all numbers (power of 2).
# Track how many random numbers have been returned.
found = 0
while found < maximum:
# If this is a valid value, yield it in generator fashion.
if value < maximum:
found += 1
yield mapping(value)
# Calculate the next value in the sequence.
value = (value*multiplier + offset) % modulus
此函数“random_range”的用法与任何生成器(如“range”)相同。 一个例子:
# Show off random range.
print()
for v in range(3,6):
v = 2**v
l = list(random_range(v))
print("Need",v,"found",len(set(l)),"(min,max)",(min(l),max(l)))
print("",l)
print()
Required 8 cycles to generate a sequence of 8 values.
Need 8 found 8 (min,max) (0, 7)
[1, 0, 7, 6, 5, 4, 3, 2]
Required 16 cycles to generate a sequence of 9 values.
Need 9 found 9 (min,max) (0, 8)
[3, 5, 8, 7, 2, 6, 0, 1, 4]
Required 16 cycles to generate a sequence of 16 values.
Need 16 found 16 (min,max) (0, 15)
[5, 14, 11, 8, 3, 2, 13, 1, 0, 6, 9, 4, 7, 12, 10, 15]
Required 32 cycles to generate a sequence of 17 values.
Need 17 found 17 (min,max) (0, 16)
[12, 6, 16, 15, 10, 3, 14, 5, 11, 13, 0, 1, 4, 8, 7, 2, ...]
Required 32 cycles to generate a sequence of 32 values.
Need 32 found 32 (min,max) (0, 31)
[19, 15, 1, 6, 10, 7, 0, 28, 23, 24, 31, 17, 22, 20, 9, ...]
Required 64 cycles to generate a sequence of 33 values.
Need 33 found 33 (min,max) (0, 32)
[11, 13, 0, 8, 2, 9, 27, 6, 29, 16, 15, 10, 3, 14, 5, 24, ...]
此答案中提出的解决方案有效,但如果样本量小但人口众多(例如random.sample(insanelyLargeNumber, 10)
),它可能会出现内存问题。
为了解决这个问题,我会这样做:
answer = set()
sampleSize = 10
answerSize = 0
while answerSize < sampleSize:
r = random.randint(0,100)
if r not in answer:
answerSize += 1
answer.add(r)
# answer now contains 10 unique, random integers from 0.. 100
如果您需要对非常大的数字进行采样,则不能使用range
random.sample(range(10000000000000000000000000000000), 10)
因为它抛出:
OverflowError: Python int too large to convert to C ssize_t
此外,如果random.sample
由于范围太小而无法生成您想要的项目数
random.sample(range(2), 1000)
它抛出:
ValueError: Sample larger than population
此功能解决了这两个问题:
import random
def random_sample(count, start, stop, step=1):
def gen_random():
while True:
yield random.randrange(start, stop, step)
def gen_n_unique(source, n):
seen = set()
seenadd = seen.add
for i in (i for i in source() if i not in seen and not seenadd(i)):
yield i
if len(seen) == n:
break
return [i for i in gen_n_unique(gen_random,
min(count, int(abs(stop - start) / abs(step))))]
非常大的数字的用法:
print('\n'.join(map(str, random_sample(10, 2, 10000000000000000000000000000000))))
样本结果:
7822019936001013053229712669368
6289033704329783896566642145909
2473484300603494430244265004275
5842266362922067540967510912174
6775107889200427514968714189847
9674137095837778645652621150351
9969632214348349234653730196586
1397846105816635294077965449171
3911263633583030536971422042360
9864578596169364050929858013943
范围小于请求项目数的用法:
print(', '.join(map(str, random_sample(100000, 0, 3))))
样本结果:
2, 0, 1
它也适用于负范围和步骤:
print(', '.join(map(str, random_sample(10, 10, -10, -2))))
print(', '.join(map(str, random_sample(10, 5, -5, -2))))
样本结果:
2, -8, 6, -2, -4, 0, 4, 10, -6, 8
-3, 1, 5, -1, 3
因此,我意识到这篇文章已有6年历史了,但是(通常)更好的算法性能还有另一个答案,尽管实用性较低,开销较大。
其他答案包括shuffle方法和使用集合的'try until valid'方法。
如果我们从区间0 ... N-1中随机选择K个整数而没有替换,那么shuffle方法使用O(N)存储和O(N)操作,如果我们从大N中选择小K则很烦人。set方法仅使用O(K)存储,但最坏情况O(inf)预期O(n * log(n))K接近N.(想象一下,尝试随机获取两个允许答案中的最后一个数字,已经选择999998,因为k = n-1 = 10 ^ 6)。
所以set方法适用于K~1,而shuffle方法适用于K~N。 两者都使用预期的> K RNG呼叫。
其他方式; 你可以假装做Fisher-Yates shuffle,并且对于每个新的随机选择,对你已经选择的元素执行二进制搜索操作,以找到你实际存储所有元素的数组时你将获得的值还没选好。
如果您已经选择的值是[2,4],并且您的随机数生成器在间隔(N-num_already_selected)中吐出2,那么您假装选择[0,1,3,5,6,... 。]通过计算小于已经选择的答案的值。 在这种情况下,您的第三个选定值将是3.然后,在下一步中,如果您的随机数再次为2,它将映射到5(在假装列表[0,1,5,6]中),因为(已经选择的值[2,3,4]的排序列表中的潜在索引为5,其为3)+ 2 = 5。
因此,将已选择的值存储在平衡二叉搜索树中,在每个节点存储秩(小于该值的值),从范围中选择一个随机数R(0 ... n-(已选择的数字) )。 然后像搜索一样下降树,但是你的搜索值是R加上你所在节点的等级。 到达叶节点时,将随机数添加到该节点的等级,并将总和插入到平衡二叉树中。
一旦你有了K个元素,就把它们从树上读到一个数组并随机播放(如果顺序很重要)。
这需要O(K)存储,O(K * log(K))性能以及K randint调用。
随机采样的示例实现(非随机最终排序,但您可以在O(K)shuffle之后),O(k)存储和O(k log ^ 2(k))性能(不是O(k log(k))因为我们无法为此实现自定义下降平衡二叉树):
from sortedcontainers import SortedList
def sample(n, k):
'''
Return random k-length-subset of integers from 0 to n-1. Uses only O(k)
storage. Bounded k*log^2(k) worst case. K RNG calls.
'''
ret = SortedList()
for i in range(k):
to_insert = random.randint(0, n-1 - len(ret))
to_insert = binsearch_adding_rank(ret, to_insert)
ret.add(to_insert)
return ret
def binsearch_adding_rank(A, v):
l, u = 0, len(A)-1
m=0
while l <= u:
m = l+(u-l)//2
if v + m >= A[m]:
l = m+1
m+=1 # We're binary searching for partitions, so if the last step was to the right then add one to account for offset because that's where our insert would be.
elif v+m < A[m]:
u = m-1
return v+m
并显示有效性:
如果我们正在进行渔民洗牌,已经选择了[1,4,6,7,8,9,15,16],随机数为5,我们尚未选择的数组看起来像[0 ,2,3,5,10,11,12,...],所以元素5是11.因此我们的binsearch函数应该返回11,给定5和[1,4,6,7,8,9,15 ,16]:
assert binsearch_adding_rank([1,4,6,7,8,9,15,16], 5) == 11
[1,2,3]的反函数是[0,4,5,6,7,8,...],其中第5个元素是8,所以:
assert binsearch_adding_rank([1,2,3], 5) == 8
[2,3,5]的逆是[0,1,4,6,...],其第一个元素是(仍)1,所以:
assert binsearch_adding_rank([2,3,5], 1) == 1
反向是[0,6,7,8,...],第三个元素是8,并且:
assert binsearch_adding_rank([1,2,3,4,5,10], 3) == 8
并测试整体功能:
# Edge cases:
assert sample(50, 0) == []
assert sample(50, 50) == list(range(0,50))
# Variance should be small and equal among possible values:
x = [0]*10
for i in range(10_000):
for v in sample(10, 5):
x[v] += 1
for v in x:
assert abs(5_000 - v) < 250, v
del x
# Check for duplication:
y = sample(1500, 1000)
assert len(frozenset(y)) == len(y)
del y
但实际上,对K~> N / 2使用shuffle方法,对K~ <N / 2使用set方法。
编辑:这是使用递归的另一种方式! O(k * log(n))我想。
def divide_and_conquer_sample(n, k, l=0):
u = n-1
# Base cases:
if k == 0:
return []
elif k == n-l:
return list(range(l, n))
elif k == 1:
return [random.randint(l, u)]
# Compute how many left and how many right:
m = l + (u-l)//2
k_right = 0
k_left = 0
for i in range(k):
# Base probability: (# of available values in right interval) / (total available values)
if random.random() <= (n-m - k_right)/(n-l-k_right-k_left):
k_right += 1
else:
k_left += 1
# Recur
return divide_and_conquer_sample(n, k_right, m) + divide_and_conquer_sample(m, k_left, l)
如果从 1 到 N 的 N 个数字的列表是随机生成的,那么是的,有一些数字可能会重复。
如果您想要以随机顺序从 1 到 N 的数字列表,请使用从 1 到 N 的整数填充数组,然后使用Fisher-Yates shuffle或 Python 的random.shuffle()
。
这是我制作的一个非常小的功能,希望对您有所帮助!
import random
numbers = list(range(0, 100))
random.shuffle(numbers)
此处提供的答案在时间和内存方面都非常有效,但由于它使用了高级 python 构造(例如 yield),所以有点复杂。 更简单的答案在实践中效果很好,但该答案的问题是它可能会在实际构造所需的集合之前生成许多虚假整数。 试试populationSize = 1000, sampleSize = 999。理论上,它有可能不会终止。
下面的答案解决了这两个问题,因为它是确定性的并且有些有效,尽管目前不如其他两个有效。
def randomSample(populationSize, sampleSize):
populationStr = str(populationSize)
dTree, samples = {}, []
for i in range(sampleSize):
val, dTree = getElem(populationStr, dTree, '')
samples.append(int(val))
return samples, dTree
其中 getElem、percolateUp 函数定义如下
import random
def getElem(populationStr, dTree, key):
msd = int(populationStr[0])
if not key in dTree.keys():
dTree[key] = range(msd + 1)
idx = random.randint(0, len(dTree[key]) - 1)
key = key + str(dTree[key][idx])
if len(populationStr) == 1:
dTree[key[:-1]].pop(idx)
return key, (percolateUp(dTree, key[:-1]))
newPopulation = populationStr[1:]
if int(key[-1]) != msd:
newPopulation = str(10**(len(newPopulation)) - 1)
return getElem(newPopulation, dTree, key)
def percolateUp(dTree, key):
while (dTree[key] == []):
dTree[key[:-1]].remove( int(key[-1]) )
key = key[:-1]
return dTree
最后,对于较大的 n 值,平均时间约为 15ms,如下所示,
In [3]: n = 10000000000000000000000000000000
In [4]: %time l,t = randomSample(n, 5)
Wall time: 15 ms
In [5]: l
Out[5]:
[10000000000000000000000000000000L,
5731058186417515132221063394952L,
85813091721736310254927217189L,
6349042316505875821781301073204L,
2356846126709988590164624736328L]
一个非常简单的功能也可以解决您的问题
from random import randint
data = []
def unique_rand(inicial, limit, total):
data = []
i = 0
while i < total:
number = randint(inicial, limit)
if number not in data:
data.append(number)
i += 1
return data
data = unique_rand(1, 60, 6)
print(data)
"""
prints something like
[34, 45, 2, 36, 25, 32]
"""
为了获得一个生成没有重复的随机值列表的程序,该列表是确定性的、高效的并使用基本编程结构构建,请考虑下面定义的函数extractSamples
,
def extractSamples(populationSize, sampleSize, intervalLst) :
import random
if (sampleSize > populationSize) :
raise ValueError("sampleSize = "+str(sampleSize) +" > populationSize (= " + str(populationSize) + ")")
samples = []
while (len(samples) < sampleSize) :
i = random.randint(0, (len(intervalLst)-1))
(a,b) = intervalLst[i]
sample = random.randint(a,b)
if (a==b) :
intervalLst.pop(i)
elif (a == sample) : # shorten beginning of interval
intervalLst[i] = (sample+1, b)
elif ( sample == b) : # shorten interval end
intervalLst[i] = (a, sample - 1)
else :
intervalLst[i] = (a, sample - 1)
intervalLst.append((sample+1, b))
samples.append(sample)
return samples
基本思想是跟踪间隔intervalLst
的可能值,从中选择我们需要的元素。 这是确定性的,因为我们保证在固定数量的步骤内生成样本(仅取决于populationSize
和sampleSize
)。
要使用上述函数生成我们所需的列表,
In [3]: populationSize, sampleSize = 10**17, 10**5
In [4]: %time lst1 = extractSamples(populationSize, sampleSize, [(0, populationSize-1)])
CPU times: user 289 ms, sys: 9.96 ms, total: 299 ms
Wall time: 293 ms
我们还可以与早期的解决方案进行比较(对于较低的 populationSize 值)
In [5]: populationSize, sampleSize = 10**8, 10**5
In [6]: %time lst = random.sample(range(populationSize), sampleSize)
CPU times: user 1.89 s, sys: 299 ms, total: 2.19 s
Wall time: 2.18 s
In [7]: %time lst1 = extractSamples(populationSize, sampleSize, [(0, populationSize-1)])
CPU times: user 449 ms, sys: 8.92 ms, total: 458 ms
Wall time: 442 ms
请注意,我减少了populationSize
值,因为它在使用random.sample
解决方案时会为更高的值产生内存错误(在此处和此处的先前答案中也提到过)。 对于上述值,我们还可以观察到extractSamples
优于random.sample
方法。
PS:虽然核心方法与我之前的答案相似,但在实现和方法方面进行了大量修改,同时提高了清晰度。
您可以使用Numpy库进行快速回答,如下所示 -
给定的代码片段列出了 0 到 5 之间的 6 个唯一数字。您可以调整参数以适应您的需求。
import numpy as np
import random
a = np.linspace( 0, 5, 6 )
random.shuffle(a)
print(a)
输出
[ 2. 1. 5. 3. 4. 0.]
正如我们在此处提到的random.sample
中看到的那样,它没有设置任何约束。
基于集合的方法(“如果返回值中有随机值,请重试”)的问题在于它们的运行时间由于冲突(需要另一个“重试”迭代)而不确定,尤其是当返回大量随机值时从范围。
不易出现这种非确定性运行时的替代方法如下:
import bisect
import random
def fast_sample(low, high, num):
""" Samples :param num: integer numbers in range of
[:param low:, :param high:) without replacement
by maintaining a list of ranges of values that
are permitted.
This list of ranges is used to map a random number
of a contiguous a range (`r_n`) to a permissible
number `r` (from `ranges`).
"""
ranges = [high]
high_ = high - 1
while len(ranges) - 1 < num:
# generate a random number from an ever decreasing
# contiguous range (which we'll map to the true
# random number).
# consider an example with low=0, high=10,
# part way through this loop with:
#
# ranges = [0, 2, 3, 7, 9, 10]
#
# r_n :-> r
# 0 :-> 1
# 1 :-> 4
# 2 :-> 5
# 3 :-> 6
# 4 :-> 8
r_n = random.randint(low, high_)
range_index = bisect.bisect_left(ranges, r_n)
r = r_n + range_index
for i in xrange(range_index, len(ranges)):
if ranges[i] <= r:
# as many "gaps" we iterate over, as much
# is the true random value (`r`) shifted.
r = r_n + i + 1
elif ranges[i] > r_n:
break
# mark `r` as another "gap" of the original
# [low, high) range.
ranges.insert(i, r)
# Fewer values possible.
high_ -= 1
# `ranges` happens to contain the result.
return ranges[:-1]
我找到了一种比使用range
函数(非常慢)更快的方法,并且不使用python
中的random
函数(我不喜欢random
内置库,因为当你播种它时,它会重复随机的模式数字生成器)
import numpy as np
nums = set(np.random.randint(low=0, high=100, size=150)) #generate some more for the duplicates
nums = list(nums)[:100]
这是相当快的。
import random nums=[random.randint(1,100) for i in range(10) if random.randint(1,100) not in nums] 这个怎么样?
一种直接的替代方法是使用 np.random.choice() ,如下所示
np.random.choice(range(10), size=3, replace=False)
这导致三个彼此不同的整数。 例如,[1, 3, 5], [2, 5, 1]...
import random
sourcelist=[]
resultlist=[]
for x in range(100):
sourcelist.append(x)
for y in sourcelist:
resultlist.insert(random.randint(0,len(resultlist)),y)
print (resultlist)
尝试使用...
import random
LENGTH = 100
random_with_possible_duplicates = [random.randrange(-3, 3) for _ in range(LENGTH)]
random_without_duplicates = list(set(random_with_possible_duplicates)) # This removes duplicates
快速、高效且易读。
如果有重复,此方法可以更改列表的长度。
编辑:在这里忽略我的回答。 如其他答案中所述,使用 python 的random.shuffle
或random.sample
。
import numpy as np minval, maxval, n_samples = -50, 50, 10 generator = np.random.default_rng(seed=0) samples = generator.permutation(np.arange(minval, maxval))[:n_samples] # or, if minval is 0, samples = generator.permutation(maxval)[:n_samples]
使用 jax:
import jax minval, maxval, n_samples = -50, 50, 10 key = jax.random.PRNGKey(seed=0) samples = jax.random.shuffle(key, jax.numpy.arange(minval, maxval))[:n_samples]
如果你想要的数字数量是随机的,你可以这样做。 在这种情况下,长度是您要选择的最大数字。
如果它注意到已经选择了新的随机数,它会从 count 中减去 1(因为在它知道是否重复之前添加了一个 count)。 如果它不在列表中,那么用它做你想做的事情并将它添加到列表中,这样它就不能再次被选中。
import random
def randomizer():
chosen_number=[]
count=0
user_input = int(input("Enter number for how many rows to randomly select: "))
numlist=[]
#length = whatever the highest number you want to choose from
while 1<=user_input<=length:
count=count+1
if count>user_input:
break
else:
chosen_number = random.randint(0, length)
if line_number in numlist:
count=count-1
continue
if chosen_number not in numlist:
numlist.append(chosen_number)
#do what you want here
从 win xp 中的 CLI:
python -c "import random; print(sorted(set([random.randint(6,49) for i in range(7)]))[:6])"
在加拿大,我们有 6/49 Lotto。 我只是将上面的代码包装在 lotto.bat 中并运行C:\home\lotto.bat
或只是C:\home\lotto
。
因为random.randint
经常重复一个数字,所以我将set
与range(7)
一起使用,然后将其缩短为 6 的长度。
有时,如果一个数字重复超过 2 次,则结果列表长度将小于 6。
编辑:但是, random.sample(range(6,49),6)
是正确的方法。
import random
result=[]
for i in range(1,50):
rng=random.randint(1,20)
result.append(rng)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.