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
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.
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.
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
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
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.
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.
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.
> 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 (=