You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alex Herbert (Jira)" <ji...@apache.org> on 2021/08/13 08:40:00 UTC
[jira] [Commented] (RNG-161) 是否可以动态设置seed?
[ https://issues.apache.org/jira/browse/RNG-161?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17398510#comment-17398510 ]
Alex Herbert commented on RNG-161:
----------------------------------
You can create a random generator (RNG) with any supported seed (int, long, byte[], int[], long[]) using:
{code:java}
long seed = 123;
UniformRandomProvider rng = RandomSource.create(RandomSource.KISS, seed);
{code}
The API does not allow reseeding an existing RNG. If you wish some part of your random operations to be reproducible you have two options:
# Create a new RNG with the same seed as previously
# Restore the state to a previously saved state
The second option is as follows:
{code:java}
long seed = 123;
RestorableUniformRandomProvider rng = RandomSource.create(RandomSource.KISS, seed);
RandomProviderState state = rng.saveState();
for (int i = 0; i < 10; i++) {
rng.nextLong();
}
// Reset
rng.restoreState(state);
{code}
Note that the state is not guaranteed to be transferable across versions. This is not intended to be used to serialise a generator upon shutdown and then restore at next start-up. The state should only be used within the same instance of the JVM.
> 是否可以动态设置seed?
> -------------
>
> Key: RNG-161
> URL: https://issues.apache.org/jira/browse/RNG-161
> Project: Commons RNG
> Issue Type: Improvement
> Components: client-api
> Affects Versions: 1.3
> Reporter: sun
> Priority: Major
>
> Does Commons RNG can set the seed by method,isn't use RandomSource.create,like random.setSeed() to set the seed ?
--
This message was sent by Atlassian Jira
(v8.3.4#803005)