neeze
neeze

Reputation: 3

Applying Math.random() to call arrays

Is it possible to use the Math.random() command to call random lines from an array or is there actually another method for this? Sorry if this have an obvious-answer but I am entirely new to programming. My codes as follows:

final float line_positions[][] = new float[][] {
        //X coordinate of line, Y Coordinate of hole
}
        { 600, 0.5f },
        { 900, 0.3f },
        { 1200, 0.2f },
        { 1500, 0.3f },
        { 1800, 0.1f },
        { 2100, 0.4f },
        { 2400, 0.5f }

};

......

int temp_score = 0;

for (int i = 0; i < line_positions.length; i++) {

float y = line_positions[i][Y] * ScreenHeight();
float x = ScreenX((int) line_positions[i][X] * ScreenWidth() * 0.0015f);

}

Upvotes: 0

Views: 138

Answers (1)

user3227998
user3227998

Reputation: 86

Use Random for it.

Random rnd = new Random();
int randomRow = rnd.nextInt(line_positions.length);

If you however want every row only once, then I'd suggest casting the array to list, use Collections.shuffle() to randomize and iterate over the list.

Upvotes: 2

Related Questions