Shuffle array in Unity

前端 未结 3 2103
执念已碎
执念已碎 2021-01-27 04:30

I want to shuffle the numbers in my array variable once, so I called my Shuffle method from Start().

I then attempt access the sh

3条回答
  •  没有蜡笔的小新
    2021-01-27 04:57

    There is still a problem with this algorithm. Imagine an array with values 0, 1, 2 The possible shuffles should be:

    0 1 2
    0 2 1
    1 0 2
    1 2 0
    2 0 1
    2 1 0
    

    and the probability of each case should be the same. To achieve this modify int r = _random.Next(0, n+1); Because the max value of Random.Next is exclusive (see doc)

提交回复
热议问题