Single or multiple random number generators?

AI Thread Summary
Using a single instance of the Random class in Java is generally more efficient than creating multiple instances. A single Random object conserves processor and memory resources, which is beneficial for overall application performance. Additionally, using one instance allows for the reuse of the same seed, enabling the recreation of the same sequence of random numbers for debugging and comparisons. However, the choice may depend on the specific application requirements, as certain scenarios might necessitate multiple instances for independent random number generation.
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.
 
Technology 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top