Generating random unique sequences in Java

I have a series of numbers, for example, that's it

[1,3,5,7,9]

It is possible to generate 5! This is a unique sequence of 120 of these numbers for example

1 3 5 7 9
5 1 3 9 7
7 3 9 5 1
1 7 9 5 3
... and so forth

I need to randomly generate 10 such sequences without repetition Any suggestions would be appreciated

Solution

List<Integer> template = Arrays.asList(1,9);
List<Integer> template = Arrays.asList(1,9);
Set<List<Integer>> seen = new HashSet<List<Integer>>();
for (int i = 0; i < 10; ++i) {
    List<Integer> items = new ArrayList<Integer>(template);
    do {
        Collections.shuffle(items);
    } while (!seen.add(items));
    System.out.println(items);
}

The above is all the contents of generating random unique sequences in Java collected and sorted out by programming house for you. I hope this article can help you solve the program development problems encountered in generating random unique sequences in Java.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>