Generate random non-repeating integers from a small range

后端 未结 5 762
慢半拍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:56

    Taking the idea from A. Donda but fixing the implementation:

    r=[randi(top,1,1),randi(top - 1, 1, count-1)];
    d=rem(cumsum(r)-1,top)+1;
    

    The first element of r is a randomly chosen element to start with. The following elements of r randomly choose the difference to the previous element, using modulo arithmetic.

提交回复
热议问题