What does it mean to assign Math.Random() > 0.5 to a variable?

Asked

Viewed 394 times

5

What does that mean Math.random() > 0.5; in a boolean? Here is 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");
        }
    }
}

3 answers

7


The Math.random() will generate a value between 0 and 0.999999 (tends to the limit of 1, but does not reach 1).

Soon your code snippet:

array[i] = Math.random() > 0.5;

Is checking if the value generated by the function random is greater than 0.5

If it’s bigger, it’s set true to that array position. Otherwise, arrow like false.

  • In this case it would be if the value of the traversed array no is less than 0.5 ?

  • It is not the value of the array, but the value of the comparison between a random number and 0.5

  • The value generated in the Random?

  • Yes. You have an array that stores bolean values, right? Each iteration inside the for will take care of checking whether the number that was randomly generated is less than or greater than 0.5. Depending on the value obtained, the value of that position (in this case, the position will be determined by the integer i) will be true or false.

6

The intention of the author of the code is that each element of the vector has a 50% chance of being true and 50% chance of being false.

In practice, that’s not going to be fully truth for mathematical reasons, but it is close enough.

About the code, Math.random() > 0.5 is a expression that returns a value boolean, that is, true or false.

As with any mathematical expression, you can assign the result to a variable of the same type generated by the expression.

For example, the excerpt:

boolean var = Math.random() > 0.5;

It is the "summarized" form of:

boolean var;
if (Math.random() > 0.5) {
    var = true;
} else {
    var = false;
}
  • 2

    Without wanting to criticize, after all you are a participatory user and an excellent moderator, but where does this answer what the author asked? It sounds more like a comment. - Be angry with me not :(

  • @Guilhermenascimento In my view, this is the only answer that answers the question: what does it mean to assign .... to a variable? See the accepted answer says what the code does, but it doesn’t say what it means. I imagine a lot of people reading the answer is getting no understanding why monkeys someone will compare a random number to 0.5.

  • I thought the author had not understood the >, or rather saying what are "ifs inline" (do not know if this is the most suitable name), in short, what I understood is that he wants to understand is the whole set and what would answer would be to explain the operators... Although this should be the basic principle to get started in programming, then I get confused if it is something worth taking forward. However, I will consider that the author answers if this is the case, because it seems that only he can say it. ;) Thank you

  • 1

    Num vai negativar, huh? Otherwise I open a post on meta is say that everyone on this site is boring. : P

  • I admit it has crossed my mind hehehe :) ... but the problem is the very problem is the question and not the answers.

  • @Joking aside, I think it makes sense that you’re worried. Many people also have difficulty with expressions that are not placed in a IF, for example. I improved the answer by giving an explanation of how the expression works.

  • 1

    I thought the explanation was very good and these examples were what were missing (if I understood the author’s doubt) +1

Show 2 more comments

5

It means exactly a boolean. What is the answer of the expression Math.random() > 0.5? Will be true or false, right? It will check whether the drawn number is greater than half and get the result whether it is true or false, then the answer will be stored in the appropriate variable.

Browser other questions tagged

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