简体   繁体   中英

Why Are there Two Constructors for the Random Class?

I've a question on Random class constructors. There are two constructors, one with a seed parameter and one without a seed.

What are the use cases to use Random class without a seed (parameterless constructor)? I cannot think any circumstance...

A Random will always have a seed. The only thing that using the constructor that takes a seed parameter does is allow you to use your own seed rather than the one generated for you. If you don't have a convenient way to generate a seed value on your own that will be more "random" than the generated seed (and you're not in a scenario where you need to use a particular seed value), then there's no need to supply one.

If you use the constructor with the seed, using the same seed value each time will result in the same number sequence being generated each time. This is useful for testing expected scenarios.

You can test using the default constructor (it will use a time-based seed) to test for intermittent scenarios.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM