python shuffle with a parameter以获得相同的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19306976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python shuffling with a parameter to get the same result
提问by Co Koder
import random
x = [1, 2, 3, 4, 5, 6]
random.shuffle(x)
print x
I know how to shuffle a list, but is it possible to shuffle it with a parameter such that the shuffling produces the same result every time?
我知道如何对列表进行混洗,但是是否可以使用参数对其进行混洗,以便每次混洗都产生相同的结果?
Something like;
就像是;
random.shuffle(x,parameter)
and the result is the same for this parameter.
Say parameter is 4
and the result is [4, 2, 1, 6, 3, 5]
every time.
这个参数的结果是一样的。说参数是4
,结果是[4, 2, 1, 6, 3, 5]
每次。
回答by lejlot
You can set the seed
(which accepts the parameter
) of your random generator, which will determinize your shuffling method
您可以设置随机生成器的seed
(接受parameter
),这将确定您的改组方法
import random
x = [1, 2, 3, 4, 5, 6]
random.seed(4)
random.shuffle(x)
print x
and the result should be always
结果应该总是
[2, 3, 6, 4, 5, 1]
In order to "rerandomize" the rest of the code you can simply reseed your random number generator with system time by running
为了“重新随机化”其余代码,您可以通过运行简单地使用系统时间重新设置随机数生成器
random.seed()
after your "deterministic" part of code
在代码的“确定性”部分之后
回答by Emmett Butler
From the python reference:
从 python参考:
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()
可选参数 random 是一个 0 参数函数,返回 [0.0, 1.0) 中的随机浮点数;默认情况下,这是函数 random()
You can use a lambda function that always returns the same value as a seed for shuffle
:
您可以使用始终返回与种子相同的值的 lambda 函数shuffle
:
In [7]: l = [1,2,3,4]
In [8]: random.shuffle(l, lambda: .5)
In [9]: l
Out[9]: [1, 4, 2, 3]
In [10]: l = [1,2,3,4]
In [11]: random.shuffle(l, lambda: .5)
In [12]: l
Out[12]: [1, 4, 2, 3] # same order as Out[9]
回答by abarnert
As the documentationexplains:
正如文档解释的那样:
The functions supplied by this module are actually bound methods of a hidden instance of the random.Random class. You can instantiate your own instances of Random to get generators that don't share state.
该模块提供的函数实际上是 random.Random 类的隐藏实例的绑定方法。您可以实例化自己的 Random 实例以获取不共享状态的生成器。
So, you can just create your own random.Random
instance, with its own seed, which will not affect the global functions at all:
因此,您可以random.Random
使用自己的种子创建自己的实例,这根本不会影响全局函数:
>>> import random
>>> x = [1, 2, 3, 4, 5, 6]
>>> random.Random(4).shuffle(x)
>>> x
[4, 6, 5, 1, 3, 2]
>>> x = [1, 2, 3, 4, 5, 6]
>>> random.Random(4).shuffle(x)
>>> x
[4, 6, 5, 1, 3, 2]
(You can also keep around the Random
instance and re-seed
it instead of creating new ones over and over; there's not too much difference.)
(您也可以保留Random
实例并重新seed
创建它,而不是一遍又一遍地创建新实例;没有太大区别。)
回答by Mark
Shuffling with a fixed value for random
does NOT work well! Example:
使用固定值洗牌效果random
不佳!例子:
from random import shuffle
v = sum([[k] * 100 for k in range(10)], [])
print v[:40]
shuffle(v, random = lambda: 0.7)
print v[:40]
Gives the output:
给出输出:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 8, 0, 0, 9, 0, 0, 0, 9, 0, 0, 8, 0, 0, 7, 0, 0, 0, 9, 0, 0, 7, 0, 0, 8, 0, 0, 0, 7, 0, 0, 7, 0, 0, 8, 0, 0, 0, 9]
It's similar for other seeds - not very random (at first sight anyway... hard to prove). This is because random
is not a seed - it is reused many times. Demonstration:
其他种子也类似 - 不是很随机(无论如何乍一看......很难证明)。这是因为random
它不是种子——它被重复使用了很多次。示范:
def rand_tracker():
rand_tracker.count += 1
return random()
rand_tracker.count = 0
shuffle(v, random = rand_tracker)
print 'Random function was called %d times for length %d list.' % (rand_tracker.count, len(v))
Which shows:
这表现了:
Random function was called 999 times for length 1000 list.
What you should do instead is @abarnert's suggestion:
你应该做的是@abernert 的建议:
from random import Random
Random(4).shuffle(x)
In that case a fixed value is perfectly fine.
在这种情况下,固定值是完全没问题的。
TLDR: Use the answer by @abarnert, do not use a fixed-value function for random
!
TLDR:使用@abernert的答案,不要使用固定值函数random
!