Results 1 to 13 of 13
  1. #1
    hugolord is offline Member
    Join Date
    Feb 2008
    Posts
    6
    Rep Power
    0

    Default 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 03:49 PM.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default 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

  3. #3
    hugolord is offline Member
    Join Date
    Feb 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

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

    What do you mean by (int, int)?

  5. #5
    hugolord is offline Member
    Join Date
    Feb 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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. #6
    vik09 is offline Member
    Join Date
    Aug 2008
    Location
    Bitola, Macedonia
    Posts
    3
    Rep Power
    0

    Default

    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. #7
    hugolord is offline Member
    Join Date
    Feb 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by vik09 View Post
    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. #8
    vik09 is offline Member
    Join Date
    Aug 2008
    Location
    Bitola, Macedonia
    Posts
    3
    Rep Power
    0

    Default

    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 06:47 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

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

  11. #11
    hugolord is offline Member
    Join Date
    Feb 2008
    Posts
    6
    Rep Power
    0

    Default

    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. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

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

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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

  1. help with an array.
    By f_the_cook in forum New To Java
    Replies: 7
    Last Post: 06-03-2008, 05:05 AM
  2. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 PM
  3. Array Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 03:25 AM
  4. can anyone help... 2d Array
    By Mark1989 in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 09:59 PM
  5. 2D array
    By bluekswing in forum New To Java
    Replies: 2
    Last Post: 01-15-2008, 06:57 PM

Posting Permissions

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