Javascript中的随机重复序列

时间:2016-06-07 17:31:47

标签: javascript matlab

我写了这个Matlab代码来生成随机[1 0]和[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]

这样的矢量

代码解释:

我有两个数字序列,1-0和2-0,我想在我的矢量中随机化。

  1. 首先,在a = repmat(Seq1,(nTrials/4),1); b=repmat(Seq2,(nTrials/4),1);我创建了固定数量的序列
  2. 其次,我把a和b放在一起:abcd = vertcat(a,b); % concatenate all three couples
  3. 第三,我在CouplesOrderStruct = abcd(randperm(size(abcd,1)),:);
  4. 中随机化这些序列

    结果是具有相同量的1-0和2-0的矢量,但是以随机顺序

    有没有办法用JavaScript获得相同的结果?

1 个答案:

答案 0 :(得分:1)

Sooo我刚刚为你构建了一个很好的小文档:

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参数只需要使用必须使用的序列数量,而不是结果的长度。但你只需要像

那样轻松地计算出来
randomRepeatSequence(seqlist, 8/seqlist[0].length)

(给出4,因为seqlist [0] .length = 2和8/2是4)

原始答案

您的结果是例如

vector = 2 0 1 0 2 0 1 0

我猜seq1和seq2应该包含相同的次数。 我决定采用一种易于理解的方法,即使我可以做得更短:

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);

执行此操作会给我一个控制台输出:

[ 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 ]