Pass random values to Matrix

Asked

Viewed 96 times

0

I have a matrix with 30 fixed values, I wanted to know how to generate these same 30 values, but random, if possible with limit values between (2400, 400).

How is the matrix currently:

private int[][] coordenadas = {
                { 2380, 29 },{ 2600, 59  },{ 1380, 89 },{ 780, 109},
                { 580, 139 },{ 880, 239  },{ 790, 259 },{ 760, 50 },
                { 790, 150 },{ 1980, 209 },{ 560, 45 },{ 510, 70 },
                { 930, 159 },{ 590, 80   },{ 530, 60 },{ 940, 59 },
                { 990, 30  },{ 920, 200  },{ 900, 259 },{ 660, 50 },
                { 540, 90  },{ 810, 220  },{ 900, 259 },{ 660, 50 },
                { 820, 128 },{ 490, 170  },{ 700, 30 },{ 920, 300 },
                { 856, 328 },{ 456, 320  }
        };
  • apparent each set has a higher value and a lower. 2400 would be the ceiling of the larger and 400 the ceiling of the smaller as I understood. But other information is missing, as if the first value needs to be greater than the second and the lowest value of both.

  • 1º value: from 500 to 2400, 2º value: 0 to 400 There is no need for the first or the second to be smaller... the second value is the height, which can be variable according to the height of the screen (in this case from 0 to 400)

2 answers

2


To generate the random numbers you can use libs Math or Random. I prefer, by several aspects to use Random.

the nextInt(int Limit) method generates an integer between 0 and less than or equal to the Limit. To use something like [min, max] you need to add min to the final number.

Random rand = new Random();
int m[][] = new int[30][2]; 
for(int i = 0; i < m.length; i++) { 
    m[i][0] = rand.nextInt(2400);
    m[i][1] = rand.nextInt(400);
}

// Exibindo o que foi criado
for(int i = 0; i < m.length; i++) { 
    System.out.printf(" {%d, %d} ", m[i][0], m[i][1]);  
}

Follow the Ideone

1

You can use the Math.Random function, it generates random numbers between [0.1]

int m[][] = new int[30][2]; 
for(int i = 0; i < m.length; i++) { 
   m[i][0] = (int) Math.random()*2400; //numeros de 0 a 2400
   m[i][1] = (int) Math.random()*400; //numeros de 0 a 400
}
  • thank you for Edit ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.