math.random always give 0 result

ぐ巨炮叔叔 提交于 2021-02-05 12:38:47

问题


I am using Ubuntu 14.04.3 LTS and I am studying Java from the book. I tried to follow one example on the book with Ubuntu Terminal and I'm using Sublime Text. Here is the code

import java.util.Scanner;

public class RepeatAdditionQuiz{
    public static void main(String[] args){
        int number1 = (int)(Math.random()%10);
        int number2 = (int)(Math.random()%10);

        Scanner input = new Scanner(System.in);

        System.out.print(
            "What is "+number1+" + "+number2+"?");
        int answer = input.nextInt();

        while(number1+number2 != answer){
            System.out.print("Wrong answer. Try again. What is "
                 +number1+" + "+number2+"? ");
            answer = input.nextInt();
        }
        System.out.println("You got it!");
    }
}

But the problem is, when I compiled it and executed it. It gives me result

what is 0 + 0?_

every time. It suppose to give me random number, and yes, it can be 0. But I tried to run it more than 10 times, it keeps giving me 0 + 0, when it's suppose to random from 0-9.

The result of 0 + 0 is fine when I typed 0 as a result, it gets me out of the loop

Did I miss something to make the math library works? How can I fix the randomize issue?


回答1:


Math.random() returns a double value between 0 (inclusive) and 1 (exclusive). It does not return an integer value. Therefore, when you take the number produced by Math.random() modulo 10, it returns the same double value. The final cast to int makes that value always 0.

Run the following code to see for yourself:

double random = Math.random();
System.out.println(random); // for example 0.5486395326203879
System.out.println(random % 10); // still 0.5486395326203879
System.out.println((int) (random % 10)); // outputs 0

What you really want is to use a Random object and use Random.nextInt(bound). To have a random integer between 0 and 9, you can use:

Random random = new Random();
int value = random.nextInt(10);



回答2:


Math.random();

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

This is the problem. You need to then multiply with 10, so you get a number between 0 and 10 and after that you can cast to int.




回答3:


public static double random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

(source)

It returns a double that satisfies 0.0 <= double < 1.0, therefore taking the modulo will always result in zero. I recommend the java.util.Random class to achieve what you need. Specifically Random.nextInt(int).




回答4:


The Math.random() method returns a random double that is from 0 (inclusive) to 1 (exclusive). Performing % 10 doesn't affect this value, but casting it to an int truncates any decimal portion, always yielding 0.

If you want a random number from 0-9, you can multiply Math.random() by 10, instead of taking the remainder when divided by 10.

Alternatively, you can create a java.util.Random object and call nextInt(10).




回答5:


Read the doc :

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

Therefore, you will always get less than 1, and the cast to int will round it down to 0. Use, for example, a Random object which has a nextInt(int max) method :

Random rd = new Random();
int number1 = rd.nextInt(10);
int number2 = rd.nextInt(10);



回答6:


The Math.random() method returns a Double value between 0 and 1, so you will never get a number greater or equal than 1, you will ever get a value that could be 0, but never 1. And as you are taking the residual from this value over 10, you will ever get a 0 as result.

Math.random() % 10 will always be 0, because the random method gives you 0 <= value < 1, and when the % 10 operation takes place, you will get 0.

Check here for more details (oracle documentation)




回答7:


Math.random() returns a number greater or equal than 0 and less than 1. What you are looking for is either

 int number1 = (int)(Math.random()*10);
 int number2 = (int)(Math.random()*10);

or

 Random rand = new Random();
 int number1 = rand.nextInt(10);
 int number2 = rand.nextInt(10);

Also, to get random number from given range, use this for Math.random()

int number 3 = min + (int)(Math.random() * ((max - min) + 1))

and for random.nextInt()

int number 4 = random.nextInt(max - min) + min;



回答8:


Math.random gives a double value between 0 (incl.)-1 (excl.)!



来源:https://stackoverflow.com/questions/32234832/math-random-always-give-0-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!