JavaScript中的随机重复序列 [英] Random repeated sequence in Javascript
问题描述
我编写了此Matlab代码以生成随机[1 0]和[2 0]的向量:
I wrote this Matlab code to generate a vector of random [1 0] and [2 0]:
nTrials = 8;
Seq1 = [1 0]; % sequence 1
Seq2 = [2 0]; % sequence 2
a=repmat(Seq1,(nTrials/4),1);
b=repmat(Seq2,(nTrials/4),1);
abcd = vertcat(a,b); % concatenate all three couples
CouplesOrderStruct = abcd(randperm(size(abcd,1)),:); % couples in columns
vector = (reshape(CouplesOrderStruct.',[],1))';
结果是一个矢量,例如:[1 0 2 0 2 0 1 0]
The result is a vector like: [1 0 2 0 2 0 1 0]
代码说明:
我有两个数字序列,分别是1-0和2-0,我想将它们随机分配到向量中.
I have two sequences of numbers, 1-0 and 2-0, which I want to randomize in my vector.
- 首先,在
a = repmat(Seq1,(nTrials/4),1); b=repmat(Seq2,(nTrials/4),1);
中,我创建了固定数量的序列 - 第二,我将a和b放在一起:
abcd = vertcat(a,b); % concatenate all three couples
- 第三,我在
CouplesOrderStruct = abcd(randperm(size(abcd,1)),:);
中将这些序列随机化
- First, in
a = repmat(Seq1,(nTrials/4),1); b=repmat(Seq2,(nTrials/4),1);
I create a fixed amount of sequences - Second, I put a and b together:
abcd = vertcat(a,b); % concatenate all three couples
- Third, I randomize these sequences in
CouplesOrderStruct = abcd(randperm(size(abcd,1)),:);
结果是一个具有相同数量的1-0和2-0,但随机顺序的向量
The results is a vector with the same amount of 1-0 and 2-0, but in a random order
是否可以通过JavaScript获得相同的结果?
Is there a way to get the same result with JavaScript?
推荐答案
如此,我刚刚为您构建了一个不错的小文档功能:
Sooo i just built a nice tiny documented function for you:
function randomRepeatSequence(sequences, times) {
// times has to be a multiple of sequences.length
if (times % sequences.length !== 0)
return console.log("times has to be a multiple of sequences.length");
// Remap our sequence-array so we can store the count it has been used
var seqmap = [];
for (var seqid = 0; seqid < sequences.length; seqid++)
// Push the current sequence n times; n = times/sequences.length
for (var idx = 0; idx < times/sequences.length; idx++)
seqmap.push(sequences[seqid]);
var resultmap = [];
// Now just select and remove a random sequence from our seqmap, until it is empty
while (!seqmap.length == 0) {
// Select a random element
var randomidx = Math.floor(Math.random()*seqmap.length);
var currentElement = seqmap[randomidx];
// remove the random element from seqmap...
seqmap.splice(randomidx, 1);
// .. and push it to the resultmap
resultmap.push(currentElement);
}
// now our resultmap looks like [[1],[2],[3]]... just flatten it!
var result = resultmap.reduce( function(a, b) {
return a.concat(b);
});
return result;
}
您可以像
console.log(randomRepeatSequence([[1,0], [2,0]], 4));
或者,更好地理解:
var seqlist = [
[1, 0],
[2, 0]
]
randomRepeatSequence(seqlist, 4)
请注意,times
参数仅采用必须使用的序列数量,而不是结果的长度.但是您只需要像
Please care, the times
parameter just takes the amount of sequences that have to be used, not the length of the result. But you just have to calculate that in a easy step like
randomRepeatSequence(seqlist, 8/seqlist[0].length)
(给出4,因为seqlist [0] .length = 2且8/2为4)
(giving 4, because seqlist[0].length = 2 and 8 / 2 is 4)
原始答案
例如,您的结果
vector = 2 0 1 0 2 0 1 0
我猜seq1和seq2应该包含相等的时间. 我决定使用一种易于理解的方法,即使可以做得更短:
I guess seq1 and seq2 should be contained equal times. I decided to use an easy-to-understand-approach, even through I can do shorter:
var trials = 8; // has to be even
var seq1 = [1, 0];
var seq2 = [2, 0];
// "Build" a sequence list
var seqlist = [
seq1, seq1,
seq2, seq2
]
var list = []
for (var n = 0; n < trials/2; n++) {
// search a random entry
var index = Math.floor(Math.random()*seqlist.length);
var toUseSeq = seqlist[index];
// delete the entry
seqlist.splice(index, 1);
list.push(toUseSeq);
}
// flatten result array
var result = list.reduce( function(a, b) {
return a.concat(b);
});
console.log(result);
执行此操作后,我得到了以下控制台输出之一:
Executing this gaves me one of these console outputs:
[ 2, 0, 1, 0, 2, 0, 1, 0 ]
[ 2, 0, 1, 0, 2, 0, 1, 0 ]
[ 1, 0, 1, 0, 2, 0, 2, 0 ]
[ 1, 0, 2, 0, 1, 0, 2, 0 ]
这篇关于JavaScript中的随机重复序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!