Results 1 to 3 of 3
  1. #1
    Dirk94 is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Generate random numbers that haven't been generated before.

    Hello,

    I'm relatively new to Java and programming, and I've come accross a problem.
    I'm having a 2-dimensional array which holds an Object.

    Java Code:
    public DataItem[][] dataTiles = new DataItem[tileWidth][tileHeight];
    The DataItem class has only two variables a x and a y value.
    Each array index stands for a position in the world, so if you would have a DataItem with x = 4 and y=32 then it's place in the array would be: dataTiles[4][32].

    The position of the dataItem is randomly generated:

    Java Code:
    int randX = (int)(Math.random()*tileWidth);
    int randY = (int)(Math.random()*tileHeight);
    
    DataItem dat = new DataItem(randX, randY);
    dataTiles[randX][randY] = dat;
    However the problem is that there is a change that there are two data items at the same coördinates.
    This is not allowed to happen, so these randX and randY variables can only be a empty place in the world.

    Could anybody point me in the right direction of how to do such a thing?

    P.S. I hope that I made the problem clear if not just ask :)
    Last edited by Dirk94; 11-26-2012 at 12:34 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default Re: Generate random numbers that haven't been generated before.

    Sounds like you're going to have to keep track of which coordinates you've already used, then check against it when creating a new one. Or you could go the other way around and start out with a collection of valid locations, then remove them or mark them as invalid as you place coordinates.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default Re: Generate random numbers that haven't been generated before.

    I think we've had something like this before, and one solution was a List<Coordinates> (in your case), and using Collections.shuffle().
    Then simply iterate over the shuffled list, assigning DataItems to the coordinates.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Store a random generated int value in an array.
    By kcon90 in forum New To Java
    Replies: 5
    Last Post: 10-26-2012, 09:31 PM
  2. Generate weighted random numbers, nextGaussian ()
    By graympa in forum New To Java
    Replies: 2
    Last Post: 03-25-2011, 09:36 PM
  3. generate pseudo random numbers in java
    By csr81 in forum Advanced Java
    Replies: 3
    Last Post: 03-01-2010, 07:08 AM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. [SOLVED] auto generated numbers
    By suprabha in forum Advanced Java
    Replies: 10
    Last Post: 08-14-2008, 05:13 AM

Posting Permissions

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