Generate random non-repeating integers from a small range

后端 未结 5 750
慢半拍i
慢半拍i 2020-12-10 08:16

What I\'m trying to accomplish is the following:

I wish to create a vector of integers, from a relatively small range, and ensure that none of the integers will be f

5条回答
  •  时光说笑
    2020-12-10 08:55

    How this?

    top = 5;
    count = 100;
    n1 = nan;
    out = [];
    for t = 1: count 
        n2 = randi(top);
        while n1 == n2
            n2 = randi(top);
        end
        out = [out, n2];
        n1 = n2;
    end
    

提交回复
热议问题