Results 1 to 13 of 13
- 08-09-2008, 01:54 PM #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.
- 08-09-2008, 02:02 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?
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 08-09-2008, 02:22 PM #3
Member
- Join Date
- Feb 2008
- Posts
- 6
- Rep Power
- 0
- 08-09-2008, 03:32 PM #4
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 #5
Member
- Join Date
- Feb 2008
- Posts
- 6
- Rep Power
- 0
- 08-09-2008, 04:40 PM #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.
- 08-09-2008, 04:52 PM #7
Member
- Join Date
- Feb 2008
- Posts
- 6
- Rep Power
- 0
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 #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:
The first elementint[here][] will be the number of coordinates, and the second will the the coordinates.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();
}
}Last edited by vik09; 08-09-2008 at 05:47 PM.
-
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 #10
There exists a class: Point that will hold the x,y.
- 08-10-2008, 01:01 AM #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
-
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++) { 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 #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));
Similar Threads
-
help with an array.
By f_the_cook in forum New To JavaReplies: 7Last Post: 06-03-2008, 04:05 AM -
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:08 PM -
Array Help
By bluegreen7hi in forum New To JavaReplies: 2Last Post: 03-28-2008, 02:25 AM -
can anyone help... 2d Array
By Mark1989 in forum New To JavaReplies: 2Last Post: 03-12-2008, 08:59 PM -
2D array
By bluekswing in forum New To JavaReplies: 2Last Post: 01-15-2008, 05:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks