Problems with generating a Math.random number, either 0 or 1

I have seen this question many times on this website and, believe me, I have watched so many of them and so many videos on YouTube, and nothing has finished working for me. As the name says, I want a random number, either 0 or 1, and then it will be returned by main (), as in my code below.

import java.util.Scanner; public class Exercise8Lab7 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int numFlips = 0; int heads = 0; int tails = 0; String answer; System.out.print("Please Enter The Number Of Coin Tosses You Want: "); numFlips = input.nextInt(); for(int x = 1;x <= numFlips; x++){ if(coinToss() == 1){ answer = "Tails"; tails++; } else{ answer = "Heads"; heads++; } System.out.print("\nCoin Toss " + x + ": " + answer); } System.out.println("\n\n====== Overall Results ======" + "\nPercentage Of Heads: " + (heads/numFlips)*100 + "\nPercentage Of Tails: " + (tails/numFlips)*100); } public static int coinToss(){ double rAsFloat = 1 * (2 + Math.random( ) ); int r = (int)rAsFloat; return r; } } 

Many solutions were suggested to use the util.Random parameter, which I did and works fine, but I want to figure out why I cannot get this to work. Obviously, I want the number to be int int, so I convert it to int after creating a random number. But no matter what I add or multiply Math.random (), it will always be either Heads, or all or be Tails. Never mixed up.

Any help appreciated!

+5
source share
9 answers

You can use boolean values ​​0 or 1 depending on the value of Math.random() as double from 0.0 to 1.0 and make the random number generator much easier. And you can completely get rid of the coinToss() method.

 if(Math.random() < 0.5) { answer = "Tails"; tails++; } 

Remove the coin toss method and replace the first conditional code above.

Math.random(); It will return a value from 0.0 to less than 1.0. If the value is in the lower half, [0,0, 0,5), then it has the same probability of being in the upper half, [0,5, 1,0]. Therefore, you can set any value in the lower half as true and upper as false.

+14
source

Try this) It will generate the number 0 or 1

  Math.round( Math.random() ) ; 
+7
source

its not working due to using integer math, calling in 2+ Math.Random pretty much always gives you an answer between 0.0 and 1.0.

assuming you get 0.25 as a result, your math is as follows

 double d = 1* (2 + 0.25); // (result = 2 

Then you check if your result is == 1 (which it will never be.)

The best result would be to declare java.util.Random as a class variable and call random.nextBoolean () and just do the calculation of your heads / tails.

If you keep using Math.random () and say

 return Math.random() < 0.5 

Your results will be slightly distorted due to the fact that Math.random () cannot return 1.0, due to the fact that the Java API specification states:

"Returns a double value with a positive sign greater than or equal to 0.0 and less than 1.0."

+1
source

Math.random() returns a random float in the range [0.0,1.0) - this means that the result can be any value from 0 to, but not including 1.0.

Your code

 double rAsFloat = 1 * (2 + Math.random( ) ); 

will accept this number in the range [0.0,1.0]; adding 2 to it gives you a number in the range [2.0.3.0]; multiplying it by 1, nothing is useful; then when you truncate it to an integer, the result is always 2.

To get integers from this kind of random functions, you need to figure out how many integers you could return, and then multiply a random number by them. If you want to get the answer "0 or 1", your range will consist of two different integers, so multiply Math.random() by 2:

 double rAsFloat = 2 * Math.random(); 

This gives you a random number in the range [0.0,2.0), which can be 0 or 1 when you trim the integer with (int) . If instead you need something that returns 1 or 2, for example, you would just add 1 to it:

 double rAsFloat = 1 + 2 * Math.random(); 

I think you already realized that the Random class gives you what you want a lot easier. I decided to explain all this, because someday you can work on an outdated system in some old language, where you really need to work with a random value [0.0,1.0). (OK, maybe this is not very likely, but who knows.)

+1
source

(int) (Math.random () * 2) also works fine in this case

+1
source

The problem can be translated into a logical generation as follows:

 public static byte get0Or1 { Random random = new Random(); boolean res= random.nextBoolean(); if(res)return 1; else return 0; } 
0
source

It is strange that no one uses modulo division for a random number. This is the easiest implementation you can get:

 Random rand = new Random(); int randomValue = rand.nextInt() % 2; 
0
source

Math.round (Math.random ()) will return either 0.0 or 1.0. Since both of these values ​​are within the range of int, they can be converted to int.

 public static int coinToss(){ return (int)Math.round(Math.random()); } 
0
source
  for(int i=0;i<100;i++){ System.out.println(((int)(i*Math.random())%2)); } 

use mod, it will help you!

-1
source

Source: https://habr.com/ru/post/1212922/


All Articles