Results 1 to 4 of 4
  1. #1
    acidacid is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default generating random numbers in a 5x5 array.

    hello. :]

    so, I've been trying to figure out for the longest time how to generate numbers in my 5x5 matrices without having the numbers repeat themselves more than once.

    Java Code:
      void Card() {
        salida = "";
        salida2 = "";
    
        for (i = 0; i <= 4; i++) {
          for (j = 0; j <= 4; j++) {
            card1[i][j] = 1 + (int) (Math.random() * 99);
            if (card1[i][j] <= 9) {
              cadena = "0" + card1[i][j];
            }
            else {
              cadena = "" + card1[i][j];
            }
            salida = salida + " " + cadena;
            if (j == 4) {
              salida = salida + "\n";
            }
          }
        }
    
        JOptionPane.showMessageDialog(null, salida, "Card 1",
                                      JOptionPane.INFORMATION_MESSAGE);
    }
    this what I have for the array. I've had plenty more, but I tried to many different methods to solve my problem, and none of it really helped. your help would be greatly appreciated. :]

    oh and I apologize about the spanish variables, but i'm still sure the code still is understandable. :]

  2. #2
    dmacvittie is offline Member
    Join Date
    Aug 2007
    Posts
    30
    Rep Power
    0

    Default

    Since this all looks okay, I presume it is the non-repeating part you're struggling with?

    The best solution to that problem AFAIK is brute force - write a method that loops through the list and returns true if it is a duplicate or false if not. put that routine and the generation of the next random into a loop until it returns false, and only then assign the value into the array.
    Java Code:
    boolean isDuplicate = true;
    while(isDuplicate) {
      card[i][j]=1 + (int) (Math.random() *99);
      isDuplicate = checkDuplicate(card1, i, j);
    }
    Then the isDuplicate method has to walk the array and compare the element at i,j with everything until it either finds a match (returning true) or runs out of elements (returning false).

    No worries about variable names ;). I don't apologize for making you read them in English, you shouldn't for returning the favor.

    Hope that helps,
    Don.
    Don MacVittie F5 Networks - DevCentral

  3. #3
    acidacid is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by dmacvittie View Post
    Java Code:
    boolean isDuplicate = true;
    while(isDuplicate) {
      card[i][j]=1 + (int) (Math.random() *99);
      isDuplicate = checkDuplicate(card1, i, j);
    }
    umm... checkDuplicate is giving me a "cannot resolve symbol." is there any way I can initialize it or declare it as a variable or something? : D

    other than that I understood everything perfectly, and I thank you very much for the help you provided. <333

  4. #4
    dmacvittie is offline Member
    Join Date
    Aug 2007
    Posts
    30
    Rep Power
    0

    Default

    You have to write it, just iterate through the array and look for a match with the new number.

    Don.
    Don MacVittie F5 Networks - DevCentral

Similar Threads

  1. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  2. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:56 AM
  3. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 05:31 AM
  4. random numbers without using java funtions
    By carlos123 in forum New To Java
    Replies: 8
    Last Post: 11-16-2007, 11:13 AM
  5. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 07:30 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
  •