How to generate a random permutation in Java?

后端 未结 3 1217
小鲜肉
小鲜肉 2020-11-30 08:59

What is the best way to generate a random permutation of n numbers?

For example, say I have a set of numbers 1, 2 and 3 (n = 3)

Set of all possible permutati

3条回答
  •  日久生厌
    2020-11-30 09:05

    You can try RubyCollect4J

    Ruby.Array.of(1, 2, 3).permutation().toA().sample();
    

    It did exactly what you asked for. BTW, I am the author of this Java library.

提交回复
热议问题