# Randomizing an array

Ranch Hand
Posts: 112
I'm not sure if this is a question better situated for the applet board, however this section seems to get more action.
I've created an array of objects that draws bars of varying heights.
for (int i = 0; i < sortsBar.length; i++){
sortsBar[i] = new Bar (x, y, width, height);
}

for (int i = 1; i < sortsBar.length - 1; i++){
x+= width + gap;
height +=5;
}
What I would like to be able to do is to randomize the array because later in the applet I will need to use two sort methods on the array. I've created code which I think should work, but doesn't.
public void paint (Graphics g){
for (int index = 0; index < 100; index++){
swap = (int) (rand.nextDouble() * 50);
swap2 = (int) (rand.nextDouble() *50);

if (swap != swap2){
myBar = sortsBar[swap];
sortsBar[swap] = sortsBar[swap2];
sortsBar[swap2] = myBar;
}
}
g.setColor (Color.white);
for (int index = 0; index <sortsBar.length; index++){
g.drawRect(sortsBar[index].x,sortsBar[index].y -sortsBar[index].height,sortsBar[index].width,sortsBar[index].height );
}
}
The output I get is a succession of bars from smallest to largest. For some reason my attempt to jumble the bars is not working even though it compiles and I have no problems running the applet.
Any help would be appreciated.
Regards, Michael

Jim Yingst
Wanderer
Sheriff
Posts: 18671
When you swap two elements, you're swapping their positions in the array. However, each object still retains its old values of x, y, width, and height, which is what determines where they are drawn onscreen. I recommend that when you swap two elements, swap only the heights for each. Leave everything else in place.
Also, you may find this link interesting.

Ranch Hand
Posts: 112
Well perhaps my reasoning is wrong, but in swapping two elements in the array, I would think I would be obchieving my objective, which is to move the objects around maintaining their data, but having them draw in a different order then the sequential order that they were originally in.
I will check out the link you suggested to see if it will bring about some clarity.
Regards, Michael

Originally posted by Jim Yingst:
When you swap two elements, you're swapping their positions in the array. However, each object still retains its old values of x, y, width, and height, which is what determines where they are drawn onscreen. I recommend that when you swap two elements, swap only the heights for each. Leave everything else in place.
Also, you may find this link interesting.

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Consider the folllowing set of statements:
Draw a point at (1, 5)
Draw a point at (2, 10)
Draw a point at (3, 15)
Draw a point at (4, 20)
I can read these in order, and draw a set of points forming a nice straight line. Now imagine that the statements were shuffled:
Draw a point at (2, 10)
Draw a point at (1, 5)
Draw a point at (4, 20)
Draw a point at (3, 15)
If I read each line in order and draw points accordingly, I will end up with the exact same graph. I may have drawn the points in a different order, but the final result is the same - because each statement had enough data to allow me to reconstruct the original picture, even with the statements scrambled. If I wanted to scramble the points up so they weren't in a line, I'd have to break apart the current x, y value pairs - e.g.
Draw a point at (1, 10)
Draw a point at (2, 5)
Draw a point at (3, 20)
Draw a point at (4, 15)
This is analogous to your situation. The final appearance of your graph has nothing to do with the order of elements in your array - it depends on the correlations between x values and height values in those elements. Hence, scramble the heights, not the whole objects.

Ranch Hand
Posts: 112
Success. I see the light.
Thank you very much, Michael

Originally posted by Jim Yingst:
Consider the folllowing set of statements:
Draw a point at (1, 5)
Draw a point at (2, 10)
Draw a point at (3, 15)
Draw a point at (4, 20)
I can read these in order, and draw a set of points forming a nice straight line. Now imagine that the statements were shuffled:
Draw a point at (2, 10)
Draw a point at (1, 5)
Draw a point at (4, 20)
Draw a point at (3, 15)
If I read each line in order and draw points accordingly, I will end up with the exact same graph. I may have drawn the points in a different order, but the final result is the same - because each statement had enough data to allow me to reconstruct the original picture, even with the statements scrambled. If I wanted to scramble the points up so they weren't in a line, I'd have to break apart the current x, y value pairs - e.g.
Draw a point at (1, 10)
Draw a point at (2, 5)
Draw a point at (3, 20)
Draw a point at (4, 15)
This is analogous to your situation. The final appearance of your graph has nothing to do with the order of elements in your array - it depends on the correlations between x values and height values in those elements. Hence, scramble the heights, not the whole objects.