-3

I am looking to set a random value to a certain element in a 2d array. In my assignment I have to assign a random number 1-5 for the first element in the 2d array and then go the the next row and do the same thing. This is what I have so far but it does not look right.

double CURRENT_BOARD [5][5] = {{0, 0, 0, 0, 0},
                          {0, 0, 0, 0, 0},
                          {0, 0, 0, 0, 0},
                          {0, 0, 0, 0, 0},
                          {0, 0, 0, 0, 0}};
  public double shuffleBoard (double currentBoard) {
  Random rand = new Random();
  shuffledBoard [][] = new double [5][5];
  for (i=0 ;i<5 ;i++) {
    j = rand.nextInt(5) + 1;
    shuffledBoard = shuffledBoard [j][0];
  }
  return shuffleBoard;
 }//shuffleBoard

My end goal is that the elements of the array will look something like {5, 0, 0, 0, 0} {3, 0, 0, 0, 0} {4, 0, 0, 0, 0} and so on as long as the first element of the array is selected at random. Can anyone offer any help to make this happen?

1 Answers1

0

Your declaration of a 2D double array is not correct. Take a look at this StackOverflow answer

Declaration should be like

double[][] CURRENT_BOARD = new double[][]{{0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0}};

For random number part, you can try this

public double[][] shuffleBoard(double[][] currentBoard) {
    double[][] shuffledBoard = currentBoard;
    Random rand = new Random();
    double rangeMin = 1, rangeMax = 5;
    for (int i = 0; i < 5; i++) {
        double j = rangeMin + (rangeMax - rangeMin) * rand.nextDouble();
        shuffledBoard[i][0] = j;
    }
    return shuffledBoard;
}
rimonmostafiz
  • 1,341
  • 1
  • 15
  • 33
  • Getting closer. Wen I run the code now I am getting an incompatible types error. If you could take a look at this it would be great. https://ideone.com/v0Ri9a – Sam Roehrich Dec 14 '17 at 06:10
  • at line 20. `public static double shuffleBoard (double[][] currentBoard)` change `double` to `double[][]`. Should work. – rimonmostafiz Dec 14 '17 at 06:15
  • IT WORKS!! Thanks! Final question though. The elements in the array go to the 15th decimal. How do I make it a whole number? Change it back to the int data type? – Sam Roehrich Dec 14 '17 at 06:31