random.shuffle随机性 [英] random.shuffle Randomness

查看:87
本文介绍了random.shuffle随机性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为家庭作业编写一种遗传算法,以解决旅行商问题.

I am trying to write a genetic algorithm for homework to solve the travelling salesman problem.

我要尝试的一种变异函数是在游览中使用random.shuffle.

One of the mutation functions that I'm trying is to use random.shuffle on the tour.

阅读random.shuffle的文档时,我看到:

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)

推荐答案

random参数用于指定(另一个)随机数生成器.它是一个期望返回范围为lt <= x <1

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<1

如果从随机数生成器两次返回相同的数字,则随机播放将是相同的.例如,

If the same number is returned twice from the random number generator, the shuffle will be the same. For example,

def mynonrandom():
 return 0.1

q
[1, 2, 3, 4]
random.shuffle(q, mynonrandom)
q
[2, 3, 4, 1]
random.shuffle(q, mynonrandom)
q
[3, 4, 1, 2]

请注意,在这种特殊情况下,我每次都会得到-1的偏移量.确切地说,您从随机输入中获得的信息可能取决于random.shuffle的实现.

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.

对于遗传算法,您希望能够具有随机大小的随机大小.使用random.shuffle不能做到这一点.您可能需要定义一些示例性变化(例如,与距离N交换成对),然后随机化(使用您要定义的某些参数)对每个新基因集执行多少操作.

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.

这篇关于random.shuffle随机性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆