1
I need to generate random numbers within a range... so far so good, however, I need to do this for each position of the array, for example, at position 1 of my array, random numbers should be inserted in a range of 10 to 20, at position 2, random numbers in a range of 30 to 40 and so on. The initial and final position will already be filled.
-- Excerpt from the code --
Random r = new Random();
int v[] = new int[capacity];
v[0] = 0;
v[capacity - 1] = 15;
for (int i = 1; i < v.length - 1; i++) {
switch (i) {
case 1:
v[i] = 1 + r.nextInt(2);
break;
case 2:
v[i] = 4 + r.nextInt(3);
break;
case 3:
v[i] = 8 + r.nextInt(3);
break;
case 4:
v[i] = 12 + r.nextInt(2);
break;
}
The method
nextInt
classRandom
already allows you to specify an upper limit. Adding something to gives an upper and lower limit. Take advantage and put the code you are using for the problem, so that the community can help you in the doubt you have.– Isac
Perfect Isac... the problem is that I get the size of the array per parameter, in the example above, I work with a 6 position, but I will have to handle 5 and 8 positions as well...
– fudo