random.shuffle 随机性
我正在尝试为作业编写遗传算法来解决旅行商问题。
我正在尝试的变异函数之一是在游览中使用random.shuffle
。
当我阅读 random.shuffle 的文档时,我看到:
shuffle(self, x, random=None, int=<type 'int'>) method of random.Random instance
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.
有人可以解释一下这个函数中“random”参数的功能吗? 我已阅读这个问题,但它没有回答我的问题。
如果我能以某种方式控制洗牌的随机性(如果这有意义的话),我特别想使用这个函数
I am trying to write a genetic algorithm for homework to solve the travelling salesman problem.
One of the mutation functions that I'm trying is to use random.shuffle
on the tour.
When I read the documentation for random.shuffle
, I see:
shuffle(self, x, random=None, int=<type 'int'>) method of random.Random instance
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.
Could someone please explain the function of the "random" parameter in this function?
I have read this question, but it doesn't answer my question.
I would especially like to use this function if I can somehow control how random the shuffling would be (if that makes any sense)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
random 参数用于指定(另一个)随机数生成器。它是一个期望返回 0<=x<1 范围内的均匀随机数的函数。
如果随机数生成器两次返回相同的数字,则随机播放将是相同的。例如,
请注意,在这种特殊情况下,我每次的偏移量都是 -1。随机输入的确切结果可能取决于 random.shuffle 的实现。
对于遗传算法,您希望能够具有可变大小的洗牌随机性。 random.shuffle 没有做到这一点。您可能需要定义一些示例更改(例如,距离 N 交换的配对),然后随机化(使用您将定义的一些参数)对每组新基因执行的操作数量。
The
random
argument is used for specifying (another) random number generator. it's a function expected to return uniform random numbers in the range 0<=x<1If the same number is returned twice from the random number generator, the shuffle will be the same. For example,
note that in this particular case I got a shift of -1 each time. Exactly what you get for a random input may depend on the implementation of random.shuffle.
For a genetic algorithm you want to be able to have variable sized randomness of the shuffle. That is not done with random.shuffle. You probably need to define some example changes (pairs with distance N swapping for example) and then randomize (with some parameters you'll define) how many of those operations you perform for each new set of genes.
shuffle
库例程允许您提供随机数源(如果不这样做,它将使用内置默认值)。因此,从理论上讲,如果您有更好的随机数来源 - 也许是连接到盖革计数器或“白噪声”广播电台的东西,或者从 random.org 获取数字的东西 - 您可以使用它。可能更有用,如果您正在测试,您可以连接一个返回相同数字序列的源,这将确保您的测试用例是可重复的并且每次总是生成相同的随机播放。理论上,您可以开发一个随机数例程,使您能够准确控制洗牌的洗牌次数;但这意味着您需要确切地了解
shuffle
的工作原理,以及必须返回哪些数字才能将结果“引导”到它们想要去的地方。理论上......这并不太困难,所使用的算法有很好的记录(Fisher-Yates),并且在调用过程中它会生成n-1
随机数,第一个从中选择一个元素索引 0 到 n-1,与最后一个元素交换,第二个从 0 到索引 n-2 选择一个元素,与最后一个元素交换,依此类推。所以,是的,您可以使用它来控制随机播放,就像@JohanLundberg示例强制随机播放一样 - 但很难使用它来控制随机播放(自从每次迭代以来,所有原始数据都在跳跃)。简短的回答,如果您需要对洗牌进行特定约束,例如“仅交换相邻元素” - 您最好实现自己的,也许使用洗牌源代码作为指导。
The
shuffle
library routine lets you provide a source of random numbers (if you don't, it'll use the built-in default). So, in theory, if you have a better source of random numbers - maybe, something connected to a Geiger counter or a "white noise" radio station, or something getting numbers from random.org - you could use that instead. Probably more useful, if you're testing, you could connect a source that returns the same sequence of numbers, which would make sure your test case is repeatable and always generates the same shuffle every time.You could, in theory, develop a random number routine that will allow you to control exactly how much shuffling the shuffle does; but that means you need to know exactly how
shuffle
works and exactly what numbers you'd have to return to "guide" your results to where they want to go. In theory... it's not too difficult, the algorithm used is very well-documented (Fisher-Yates), and during the call it generatesn-1
random numbers, the first to select an element from index 0 to n-1, to swap with the last element, the second to select an element from 0 to index n-2, to swap with the last-but-one element, and so on. So yes, you could use this to control the shuffle, like @JohanLundberg example which forces the shuffle to shift - but it would be very hard to use that to control the shuffle (since each iteration, all the original data is jumping around).Short answer, if you need specific constraints on a shuffle, such as "only swap neighboring elements" - you'll be better off implementing your own, perhaps using the shuffle source code as a guide.
正如 Johan 所说,随机参数只是为 shuffle 函数提供了使用的随机性。因此您可以使用默认值或提供您自己的。因此,如果你希望它更加完全随机,你可以去找一个比 python 更好的库。
根据您对我的评论的回复,听起来您正在寻找距离而不是随机性。您可能想要进行一组 n 次随机洗牌并最大化距离。要计算距离,您可以使用一些类似的技术,我还没有寻找这些技术的实现,但是肯定存在。
As Johan said, the random parameter simply provides the randomness for the shuffle function to use. So you can use the default or provide your own. So if you want it to be more perfectly random, you could go and find a library that does better than the python one.
Based on your reply to my comment, it sounds like you are looking for distance instead of randomness. You might like to take a set of n random shuffles and maximize the distance. To calculate distance, you can use some techniques like these which I haven't looked for implementations of but certainly exist.