-1

my question is....How to assign random numbers to the matrix of Buttons [5][5]. The numbers will be ranging from 1 to 25.

  • Don't beg for an answer, don't beg for an answer. Fill an `ArrayList` with your range. Shuffle it. Then fill your array of arrays. If you need more help, post code. And try using words without repeating yourself. **Without** repeating yourself. – Elliott Frisch Mar 29 '20 at 20:13
  • Please have a look at [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – mixable Mar 29 '20 at 20:13
  • They have this new thing called a **web search**. It's really amazing what you can find using it. E.g. if you use it to search for [`java random numbers`](https://www.google.com/search?q=java+random+numbers) it magically provides you with a list of articles on the web that shows you how to generate random numbers using Java code. – Andreas Mar 29 '20 at 21:12

2 Answers2

0

Use the java utility Random to populate a number 1-25 using

rand.nextInt(25);

generates a number 0-24, so add one to it before assigning the integer to buttons index.

import java.util.Random;
class solution
{
    public static void main (String[] args)
    {
        Random rand = new Random();
        int[][] buttons = new int[5][5];
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                buttons[i][j] = rand.nextInt(25) + 1;
            }
        }
    }
}
Garrett
  • 319
  • 1
  • 13
0
 Random rand = new Random(); 

    for (int i = 0; i < 5; i++) {     
        for (int j = 0; j < 5; j++) {
            int r = rand.nextInt(25) + 1; 
            buttons[i][j] = r;
        }

    }
Nilesh B
  • 937
  • 1
  • 9
  • 14