h*****y 发帖数: 68 | 1 假设我有1000个trials,有8个conditions,要随机这些trials。
看起来很简单,shuffle了之后取余。但是还要求紧邻的两个trials不能是same
condition,这就不好办了。
[这不是什么题目,而是实际遇到的问题。我把这个问题实例化为:
世界大学生运动会上有某8个国家,每个国家125名运动员,现在要让他们排队吃饭,紧
邻的两个队员不能是从同一个国家来的。为了避免国别歧视(来自同一个国家的运动员
是等同的),采用计算机随机。算法是怎样的?]
我的笨办法是,分成125个小block,每个block 8 trials
第一个block shuffle,然后取第二个block,如果首trial跟第一个block末trial相同
,就再做shuffle一次 。。。。
all = 1000;
con = 8;
block = all/con;
trials = zeros(1000,1);
trials[1:con] = shuffle(con);
for i = 1 : block - 1
do {
s[1:con] = shuffle(con);
} while (s[1] == trials[i*con])
trials[i*con+1:(i+1)*con] = s[1:con];
end
有更好的办法吗?
请教各位大牛,怎么做? | b********e 发帖数: 58 | 2 If I understand you correctly, maybe the following algorithm can work for
you?
step 1. Pick a random integer c1 in [1,2,...8]. This would be the condition
for your first trial.
step 2. Pick a random integer c2 in [1,2,..., 7], and if c2 >= c1, let c2 =
c2 + 1. This would be your condition for the second trial.
step 3. let c1 = c2, and repeat by going to step 2 until you have all 1000
trials. | h*****y 发帖数: 68 | 3 谢谢!以下是根据您的算法,matlab的代码
all = 1000;
con = 8;
trials = zeros(all,1);
c1 = randperm(8,1);
trials(1) = c1;
for i = 2 : all
c2 = randperm(7,1);
if (c2 >= c1)
c2 = c2 + 1;
end
trials(i) = c2;
c1 = c2 ;
end
但是这个算法不能保证每个condition对应的trials数相同(=125)
这是我运行某一次的结果:116 + 112 + 131 + 118 + 136 + 131 + 133 + 123 = 1000
我能想到的是在里面加一个数组full = zeros(8,1)对应每个condition,每次++一下,
如果=125了,就不再找这个对应的condition了。 | h*****y 发帖数: 68 | 4 我猜这个问题是无解的。取一个极端情况:
当condition = 2的时候只有取第一个trial时是自由的,可以随机的。
因为之后的数的选择依赖于前面的数,所以没法做到绝对随机。最理想的情况估计就是
像我一开始做的那样,每8个为一个block随机。 |
|