Question:
What does it mean Math.random() > 0.5;
in a boolean
? Here's an example:
class Pro {
public static void main(String[] args) {
int numero = 10;
boolean[] array = new boolean[numero];
for(int i= 0; i< array.length; i++) {
array[i] = Math.random() > 0.5;
System.out.print(array[i] + "\t");
}
}
}
Answer:
Math.random()
will generate a value between 0 and 0.999999 (tends to limit 1, but doesn't reach 1).
Logo your code snippet:
array[i] = Math.random() > 0.5;
It is checking if the value generated by the random
function is greater than 0.5
If it is greater, it sets true
for that position of the array. Otherwise, set to false
.