Random

J

JS

Hi all. What is the best way to create a random variable in a class using
the java.util.random class??
Is it better as a local variable or global and also I heard somewhere that
you should declare it as static to make it more random.
any pointers will be gratefully recieved.
Thanks
James
 
D

Daniel Dyer

Hi all. What is the best way to create a random variable in a class using
the java.util.random class??
Is it better as a local variable or global

Not sure what you are asking here. It is unlikely that you would want to
create a local instance of Random, unless it really was a one-off random
number that you wanted. If you create a new Random each time, even if you
seed it from the system clock (the default), you could possibly end up
using the same seed.
and also I heard somewhere that
you should declare it as static to make it more random.

Making it static will not make it "more random". I think what you're
getting at is that you usually only need one source of random numbers
(i.e. one instance of java.util.Random). This is because if you create
more than one instance there is a possibility that two instances will have
the same seed and therefore produce the same sequence of (pseudo) "random"
numbers.

Dan.
 
J

JS

thanks. sorry maybe i should have been a bit clearer. my random number needs
to create an integer which relates to an index in an arraylist. the number
should be any number between 0 and 51 so any one of 52 numbers. and yeah i
forgot about the random thing only being pseudo random. I dont really want
to complicate things by giving it a seed, just the default clock is fine. I
just wondered what peoples opinions were because I have heard several ways
of using random
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top