1. Member
Join Date
Feb 2008
Posts
6
Rep Power
0

## randomizing an array HELP! please!

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 :)
Last edited by hugolord; 08-09-2008 at 02:49 PM.

2. ## 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?

3. Member
Join Date
Feb 2008
Posts
6
Rep Power
0
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)

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

What do you mean by (int, int)?

5. Member
Join Date
Feb 2008
Posts
6
Rep Power
0
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

6. Member
Join Date
Aug 2008
Location
Bitola, Macedonia
Posts
3
Rep Power
0
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.

7. Member
Join Date
Feb 2008
Posts
6
Rep Power
0
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)

8. Member
Join Date
Aug 2008
Location
Bitola, Macedonia
Posts
3
Rep Power
0
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:
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.
Last edited by vik09; 08-09-2008 at 05:47 PM.

9. 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.

10. There exists a class: Point that will hold the x,y.

11. Member
Join Date
Feb 2008
Posts
6
Rep Power
0
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

12. A simple example of what I was talking about:
Java 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++)
{
}

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 + "]";
}

}```

13. Java 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));```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•