Setting a seed to shuffle ArrayList in Java determ

2019-02-16 05:21发布

I have a list of integers (currently using cern.colt.list.IntArrayList). I can call "shuffle()" and randomly shuffle them. I would like to be able to reproduce a shuffle. I can reproduce a series of random numbers by setting a seed. I do not seem to be able to set a seed in this case. What should I do? I am open to other implementations.

标签: java random
3条回答
我命由我不由天
3楼-- · 2019-02-16 05:41

This is possible by using the shuffle method that allows you to provide the backing Random instance: Collections.shuffle(List<?> list, Random rnd):

Example:

Collections.shuffle(yourList, new Random(somePredefinedSeed));
查看更多
SAY GOODBYE
4楼-- · 2019-02-16 05:44

You can specify the Random instance with a seed value using public static void shuffle(List list, Random rnd). For the Random(long seed) constructor you can specify a seed.

From Java Docs:

Randomly permute the specified list using the specified source of randomness. All permutations occur with equal likelihood assuming that the source of randomness is fair.

查看更多
登录 后发表回答