Why Random() produce same random number?

i have used Random() method to display my graphic(few of it) randomly which i used loop with it. but all of the random number created are the same. why is this happen? this cause the graphic overlap each other and i can only see juz 1 graphic...help plz...
[263 byte] By [ckminga] at [2007-9-28 6:24:33]
# 1
We can't help you without your code.
Viruma at 2007-7-9 17:36:29 > top of Java-index,Other Topics,Java Game Development...
# 2

Because You didn't post your code I HAVE to guess:

Guess #1:

Your probably only generating one set of coordinates and moving all your images to the same place

Guess #2:

Your only drawing 1 image.

Guess #3:

You don't understand how Random() works

Guess #4

I'm guessing #3

To quoth the documentation:

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.

Hope this helps

--Dave

narcca at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 3

for (int i=0; i<10 ; i++) {

int wallPositionX = 0 + Math.abs(new Random().nextInt())%15;

int wallPositionY = 0 + Math.abs(new Random().nextInt())%14;

label.setBounds(wallPositionX, wallPositionY, size, size);

panel.add(label);

}

the result of this codes will result all the label position to the same position of all label on the panel.

ckminga at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 4
panel.add(label[]);this is the correction for the code above...the square bracket should contain 'i'... i dunno why i can't display it...all the letter turn italic when i does it.....^
ckminga at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 5
same apply to all "label[]" in the code. in the square bracket contain the variable "i"
ckminga at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 6

This forum uses a few formatting tags - see http://forum.java.sun.com/faq.jsp#messageformat

Try changing for (int i=0; i<10 ; i++) {

int wallPositionX = 0 + Math.abs(new Random().nextInt())%15;

int wallPositionY = 0 + Math.abs(new Random().nextInt())%14;

label[i].setBounds(wallPositionX, wallPositionY, size, size);

panel.add(label[i]);

}

to Random rand = new Random();

for (int i=0; i<10 ; i++) {

int wallPositionX = 0 + Math.abs(rand.nextInt())%15;

int wallPositionY = 0 + Math.abs(rand.nextInt())%14;

label[i].setBounds(wallPositionX, wallPositionY, size, size);

panel.add(label[i]);

}

As the docs make clear, the seed used by the Random() constructor is the current time, and using the same seed guarantees the same sequence.

YATArchivista at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 7
the docs also make clear that you shouldn't use nextInt()% to get random numbers like that
markuskidda at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 8

Yes you can just use...

rand.nextInt(max-value) - e.g.Random rand = new Random();

for (int j=0; j<10 ; j++)

{

int wallPositionX = rand.nextInt(15);

int wallPositionY = rand.nextInt(14);

label[j].setBounds(wallPositionX, wallPositionY, size, size);

panel.add(label[j]);

}

Regards,

Tim

TimRyanNZa at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 9

I can think of only 1 scenario, in which you would use

Math.abs(rand.nextInt())%range

instead of

rand.nextInt(range)

would be if you were going after max. speed. (at the expense of a uniformal distribution of random numbers)

However, if max. speed was your only motive, then you would either

1) pre-calculate a fairly large set of numbers, and increment through them.

OR

2) (rand.nextInt() & 0x7FFFFFFF)%range

Abusea at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 10

The truth is that there are no true random numbers. The truth is that we can create only seemingly random numbers, it is even questionable whether we can make random numbers with a dice. But I bet the Java developers have used an algorithm that has been developed by very high mathemathicians and is able to produce quite random numbers.

OlliPekkaa at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 11
Congratulations OlliPekka, you have made a witting discovery...
Kayamana at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 12
> The truth is that there are no true random numbers.That's not quite correct. The vast majority of infinite bitstrings are random (if you use Kolmogorov randomness, which is a reasonable definition).
YATArchivista at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 13
Actualy, theres no such thing as random in the entire universe. There can not be randoms in a world where everything that happens is based on something else.
EvolvedAnta at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 14

Everything happens based on something else? I've heard there are cases in quantum phsics where it is absolutely impossible to determine the state a system will move to next, assuming today's physics theories are correct. That sounds random enough to me.

Under the assumption the whole universe and its changes through time are determined, there really would not be any "true" randomness. I think it would still be random to someone with limited perception. Let's say a random number generator uses key presses by a human and the system time to generate seed numbers, and the part of the system time where time changes below one milli second (below one nano second if you want and the hardware allows) are measured is used for the seed number, the human won't be able to predict the seed number his key stroke genrerates. From the point of view of the human, the seed number will be totally random, because he's not able to perceive the information (the nano second part of the system time) that determines the seed number or to make any useful predictions about it.

Zyphrusa at 2007-7-9 17:36:30 > top of Java-index,Other Topics,Java Game Development...
# 15

The users perception may see randomness, but that does not mean it IS random. As for the quantum physics statement, just because something is impossible to determine, does not mean that certain something is random. If there's a black box with dice in it thats impossible to open. It doesnt mean the dice is truely random number when we shake the box just because we can't see the dice.

EvolvedAnta at 2007-7-18 19:11:41 > top of Java-index,Other Topics,Java Game Development...
# 16

> The users perception may see randomness, but that does

> not mean it IS random. As for the quantum physics

> statement, just because something is impossible to

> determine, does not mean that certain something is

> random. If there's a black box with dice in it thats

> impossible to open. It doesnt mean the dice is truely

> random number when we shake the box just because we

> can't see the dice.

Have you ever heard of Schrodinger's Cat? the dice will exist in a possible states superimposed upon each other until something observes them (=

Zemthematress42a at 2007-7-18 19:11:41 > top of Java-index,Other Topics,Java Game Development...
# 17
Quantum Determinancy, an interesting topic - though a little OT :p
Abusea at 2007-7-18 19:11:41 > top of Java-index,Other Topics,Java Game Development...