# randomizing an array HELP! please!

• 08-09-2008, 01:54 PM
hugolord
Hi, well I've been cracking my head lately to how I can randomize an array..

what I've gotten so far:

Random[] Number = new Random[] { new Random(23), new Random(2)}; Random generator = new Random();

Mynumber[] Tiles = {new Array (423, 412 ), new Array (432, 435 )};
randomNumber = Math.random();
numberOfElements = Mynumber.length;
randomElement = Math.round(randomNumber * numberOfElements);

ok so what I want to do is have two numbers divided by a comma (int,int), the first method works but only allows me to use one number on each element and the second doesn't compile at all, any help/advice?

thanks alot :)
• 08-09-2008, 02:02 PM
Nicholas Jordan
routines available
There are routines available in the standard libraries, we can help you find them if needed. Are there any paticular concepts you have arrived at but cannot get started on how to use?
• 08-09-2008, 02:22 PM
hugolord
Quote:

Originally Posted by Nicholas Jordan
There are routines available in the standard libraries, we can help you find them if needed. Are there any paticular concepts you have arrived at but cannot get started on how to use?

Well I just wanted to know how to add more than one int into the array so I would have (int,int)
• 08-09-2008, 03:32 PM
Norm
int[] anArray = new int[] {1, 2, 5};
To put 3 numbers into an array.

What do you mean by (int, int)?
• 08-09-2008, 04:00 PM
hugolord
Quote:

Originally Posted by Norm
int[] anArray = new int[] {1, 2, 5};
To put 3 numbers into an array.

What do you mean by (int, int)?

Yes, I know that's how you do it but I want something like this

Random[] Number = new Random[] { new Random(23,45), new Random(24,65)};

So it would generate randomly either 23,45 OR 24,65
• 08-09-2008, 04:40 PM
vik09
You can`t give the Random constructor 2 arguments.
Look at the API . You can only give a argument if you want to get long random number.

btw Can you explain what is it exactly that you want. I can`t understand completely.
• 08-09-2008, 04:52 PM
hugolord
Quote:

Originally Posted by vik09
You can`t give the Random constructor 2 arguments.
Look at the API . You can only give a argument if you want to get long random number.

btw Can you explain what is it exactly that you want. I can`t understand completely.

well I'm making coords for this game (nvm that, it's a bit weird) and they consist of x and y so (x,y) which means two integers in one element, so I wanted to know how I could get a list of different elements like (34,345) or (34546,678876) or (2342,867) and get them all in an array and then randomly select one of them so i'd get (randomly):

34,345) or (34546,678876) or (2342,867)
• 08-09-2008, 05:08 PM
vik09
Well maybe you can use a 2Dimensional array int[][]
You can put one coord. in as the first element, and the second as the second element.
Something like this:
Quote:

int[][] coords = new int[3][2];
for (int i=0; i<=coords.length--; i++){
for(int j=0; i<=1; j++){
coords[i][j]= (int) Math.random();
}
}
The first elementint[here][] will be the number of coordinates, and the second will the the coordinates.
• 08-09-2008, 08:07 PM
Fubarable
I would create a class of objects that hold the x and y positions of each whatever. Then place these in an ArrayList, then use Collections#shuffle(...) to shuffle this arraylist.
• 08-09-2008, 08:14 PM
Norm
There exists a class: Point that will hold the x,y.
• 08-10-2008, 01:01 AM
hugolord
erm it doesn't have to be x,y Norm and vik that might work I think and Fubarable, you mind explaining that a bit further?

I think that's exactly what I need
• 08-10-2008, 02:52 AM
Fubarable
A simple example of what I was talking about:
Code:

```import java.awt.Point; import java.util.ArrayList; import java.util.Collections; public class RandomizedCollection {   public static void main(String[] args)   {     ArrayList<FubarPoint> fuPointList = new ArrayList<FubarPoint>();     String[] names =     {       "Joe", "Laurie", "Frank", "Andy", "Pete", "Diane", "Roger", "Jill", "Barbara",       "Raymond", "Hank", "Harriett"     };         for (int i = 0; i < names.length; i++)     {       fuPointList.add(new FubarPoint(names[i], new Point(i, i)));     }         System.out.println("Before Shuffle: ");     System.out.println(fuPointList);         Collections.shuffle(fuPointList);     System.out.println("After Shuffle: ");     System.out.println(fuPointList);   } } class FubarPoint {   String name;   Point point;   public FubarPoint(String name, Point point)   {     this.name = name;     this.point = point;   }   public String getName()   {     return name;   }   public Point getPoint()   {     return point;   }   @Override   public String toString()   {     return name + ": [" + point.x + ", " + point.y + "]";   }   }```
• 08-10-2008, 03:36 AM
Norm
Code:

```Random rand = new Random(System.currentTimeMillis());  // init with seed int MaxNbr = 1233434.....;                                // largest number to return  ... Point[] pts = new Point{NbrOfPts];  // define an array with NbrOfPts elements for(int i=0; i <NbrOfPts; i++)  // fill the arry with pairs of random numbers   pts[i] = new Point(rand.nextInt(MaxNbr), rand.nextInt(MaxNbr));```