0

I am very new to android programming on eclipse and I have a problem trying to figure out on how to call random values from a list in an array. My codes as follows:

final float column_positions[][] = new float[][] {
    { 600, 0.5f },
    { 900, 0.3f },
    { 1200, 0.2f }
};
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
neeze
  • 3
  • 1
  • R u luking for this http://stackoverflow.com/q/363681/3110609 or sumthing else – Harshal Benake Mar 06 '14 at 12:42
  • @HarshalBenake hi, thank you for the link but I was looking for something else but I sure did learn something new from the link as well =) Thanks again! – neeze Mar 06 '14 at 13:47

2 Answers2

0
final Random rnd = new Random();
// get random column
final float[] randomColumn = column_positions[rnd.nextInt(column_positions.length)]; 
//get random row from random column
float random = randomColumn[rnd.nextInt(randomColumn.length)];
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
-1

You can use this line for random value.

Random random = new Random();
int randomValue = random.nextInt(column_positions.length-1);
Yogendra
  • 4,817
  • 1
  • 28
  • 21
  • This is not a random value, but a random index of an single-dimentional array, but the range is cut-out by length - 1 so you're always missing the last array index. – Yaroslav Mytkalyk Mar 06 '14 at 12:52
  • Hello Doctoror, i know this is random index, after getting random index, you can easily find out value. For you kind of information, i want to say that random value always generate from 0 to given length -1. please refer this link: http://java.about.com/od/javautil/a/randomnumbers.htm – Yogendra Mar 06 '14 at 13:02
  • random.nextInt(range) generated [0, range - 1], so by decrementing the *length* you actually get [0, range - 2]. The index you called "randomValue" is a random index, but not for a value, but for a single-dimentional array. So this answer does not fully explain how to retrieve the value of two-dimentional array. – Yaroslav Mytkalyk Mar 06 '14 at 13:25