gpt4 book ai didi

Repeatedly using randperm until a standard is met always gives duplicate results on two consecutive runs(重复使用RANDERM,直到达到标准,在连续两次运行中总是会得到重复的结果)

转载 作者:bug小助手 更新时间:2023-10-26 20:53:39 26 4
gpt4 key购买 nike



We have in Matlab the magic function, which generates a magic square. However, it always gives the same square while there can be multiple possible magic squares for a given dimension.

我们在MatLab中有一个魔术函数,它可以生成一个魔方。然而,它总是给出相同的正方形,而对于给定的维度可以有多个可能的幻方。


Now, I want to generate randomly a magic square of a given dimension, and I have a piece of code here:

现在,我想随机生成一个给定维数的幻方,这里有一段代码:


while true
nums = randperm(9);
A = reshape(nums, 3, 3);
if all(sum(A, 1) == 15) && all(sum(A, 2) == 15) && sum(diag(A)) == 15 && sum(diag(fliplr(A))) == 15
break;
end
end
disp(A);

It works almost fine. It generates the magic square well enough. However, on consecutive runs it gives me the same matrix:

它几乎运行得很好。它可以很好地生成幻方。然而,在连续运行时,它给出了相同的矩阵:


>> ranMagic
8 1 6
3 5 7
4 9 2

>> ranMagic
8 1 6
3 5 7
4 9 2

>> ranMagic
6 7 2
1 5 9
8 3 4

>> ranMagic
6 7 2
1 5 9
8 3 4

>> ranMagic
6 1 8
7 5 3
2 9 4

>> ranMagic
6 1 8
7 5 3
2 9 4

>> ranMagic
6 7 2
1 5 9
8 3 4

>> ranMagic
6 7 2
1 5 9
8 3 4

>> ranMagic
2 9 4
7 5 3
6 1 8

>> ranMagic
2 9 4
7 5 3
6 1 8

>> ranMagic
2 9 4
7 5 3
6 1 8

>> ranMagic
6 7 2
1 5 9
8 3 4

>> ranMagic
6 7 2
1 5 9
8 3 4

>> ranMagic
8 1 6
3 5 7
4 9 2

>> ranMagic
8 1 6
3 5 7
4 9 2

Notice how the first two matrices are the same, then the third and forth, then the fifth and sixth... While it is true that 3D magic squares are scarce in number, with only 8 possible alternatives, this strikes me more than a mere coincidence. How come this happens?

注意前两个矩阵是如何相同的,然后是第三个和第四个,然后是第五个和第六个……虽然3D魔方的数量确实很少,只有8种可能的选择,但这给我的印象不仅仅是巧合。为什么会发生这种事?


更多回答

I have no problem believing that is coincidence.

我毫不犹豫地相信这是巧合。

@Shaobin It would be easy for you to put this in a loop of large number of trials (e.g., 1M) to see if this is coincidence or not.

@绍宾,你很容易把它放在大量试验(例如,1M)的循环中,看看这是不是巧合。

优秀答案推荐
更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com