Math.random()

A

Aaron Fude

Hi,

Is there a way to reset the seed globally so that Math.random() starts
returning the same numbers every time the program is executed or is using
java.util.Random the only way to it?

Thanks,

Aaron Fude
 
R

Roedy Green

Is there a way to reset the seed globally so that Math.random() starts
returning the same numbers every time the program is executed or is using
java.util.Random the only way to it?

you need java.util.Random. Math.random is just a shortcut.
 
A

Aaron Fude

Roedy Green said:
you need java.util.Random. Math.random is just a shortcut.

Which means that I need a class with contains a static instance of
java.util.Random (if I want the setting to be program-wide)? Wondering why
java does not provide such a class.
 
M

Michael Borgwardt

Aaron said:
Which means that I need a class with contains a static instance of
java.util.Random (if I want the setting to be program-wide)?

Not necessarily. What you most need is to initialize your Random objects
with a fixed seed.
Wondering why
java does not provide such a class.

Because it's trivial to do and not needed all that often?
 
O

Oscar kind

Aaron Fude said:
Which means that I need a class with contains a static instance of
java.util.Random (if I want the setting to be program-wide)? Wondering why
java does not provide such a class.

Java does; by way of Math.random(). It's just that if you need a seeded
random generator, there are so many possibilities that it's better to
create a singleton in your program (at least IMHO).


Oscar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top