Single or multiple random number generators?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 4K views
mtanti
Messages
172
Reaction score
0
I'm programming in Java and using the Random class and I'm not sure if it makes a difference whether I use a single accessible Random object for the whole program or if I should create multiple instances (one for each object needing it?). Are there disadvantages to using one instead of the other?

I'm assuming that it would help to be able to reuse the same seed on a single random object in order to re-create the same sequence of random numbers during debugging and comparisons.

Thanks.
 
Physics news on Phys.org
mtanti said:
I'm programming in Java and using the Random class and I'm not sure if it makes a difference whether I use a single accessible Random object for the whole program or if I should create multiple instances (one for each object needing it?). Are there disadvantages to using one instead of the other?

I'm assuming that it would help to be able to reuse the same seed on a single random object in order to re-create the same sequence of random numbers during debugging and comparisons.

Thanks.

It depends on the application. If you can get away with just one that is best. Instantiating new objects takes both processor and memory resources.