设置种子以确定性地在Java中对ArrayList进行洗牌 [英] Setting a seed to shuffle ArrayList in Java deterministically
问题描述
我有一个整数列表(目前使用 cern.colt.list。 IntArrayList )。我可以打电话给shuffle()然后随意洗牌。我希望能够重现一场洗牌。我可以通过设置种子来重现一系列随机数。在这种情况下,我似乎无法设置种子。我该怎么办?我对其他实现持开放态度。
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.
推荐答案
这可以通过使用允许您提供支持的shuffle方法来实现 Random
instance: Collections.shuffle(List<?> list,Random rnd)
:
This is possible by using the shuffle method that allows you to provide the backing Random
instance: Collections.shuffle(List<?> list, Random rnd)
:
示例:
Collections.shuffle(yourList, new Random(somePredefinedSeed));
这篇关于设置种子以确定性地在Java中对ArrayList进行洗牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!