0

I have been trying this code and the target number generated using math.random() always comes out to be zero. Is there any problem with the code? Please help. I tried a number of times, but every time i try the target random number is always zero.

public class Player {
    int number;

    public void guess()
    {
        number = (int) (Math.random()*10);
    }
}



public class GuessGame {
Player p1;
Player p2;
Player p3;

public void startGame()
{
    p1 = new Player();
    p2 = new Player();
    p3 = new Player();

    int targetNumber; 
    targetNumber = (int) Math.random() * 10 ;
    System.out.println("The target number is "+ targetNumber);


    while(true)
    {
        p1.guess();
        p2.guess();
        p3.guess();

        int guessp1 = p1.number;
        int guessp2 = p2.number;
        int guessp3 = p3.number;

        System.out.println("Number guessed by player p1 is "+ guessp1);
        System.out.println("Number guessed by player p2 is "+ guessp2);
        System.out.println("Number guessed by player p3 is "+ guessp3);

        boolean isp1 = false;
        boolean isp2 = false;
        boolean isp3 = false;

        if(targetNumber==guessp1)
            isp1 = true;
        if(targetNumber==guessp2)
            isp2 = true;
        if(targetNumber==guessp3)
            isp3 = true;
        if(isp1||isp2||isp3)
        {
            System.out.println("player1 got it right? " + isp1);
            System.out.println("player2 got it right? " + isp2);
            System.out.println("player3 got it right? " + isp3);
            System.out.println("Game Over!!!");
            break;
        }
        else
        {
            System.out.println("All Wrong!! Play Again..");
        }
    }

}

}

public class GameLauncher {
    public static void main(String[] args)
    {
        GuessGame game = new GuessGame();
        game.startGame();
    }
}
Vanellope
  • 55
  • 6
  • Avoid using `Math.random` and multiplying - use `new Random()` and `nextInt`. Use one instance of `Random` for the entire application. – Boris the Spider Jul 26 '14 at 10:16

5 Answers5

3

The problem is at below line

targetNumber = (int) Math.random() * 10 ;

Math.random() returns double value between 0 (including) to 1 (excluding) and you are casting that to int that becomes it zero before multiplication.

use

targetNumber = (int) (Math.random() * 10 );

or better use

Random random = new Random();
number = random.nextInt(10);
Braj
  • 46,415
  • 5
  • 60
  • 76
1

Due to operator precedence the cast occurs first making the first term in the assigned expression equal to 0 (since Math.random returns a value < 0). You can use parenthesis to multiply the numbers first

targetNumber = (int) (Math.random() * 10) ;

or as Boris said simply use nextInt from the java.util.Random

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

You should write

targetNumber = (int) (Math.random()*10)

This is because what you are doing converts Math.random() generated number to int as zero and then mulyiplied by 10

Niru
  • 732
  • 5
  • 9
0

You are using Math.random() which states

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

You are casting the result to an int, which returns the integer part of the value, thus 0.

Then 1 + 0 - 1 = 0.

targetNumber = (int) (Math.random() * 10) ;

Consider using Random

Random random = new Random();
number = random.nextInt(10);
Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
0

Use Random class to generate radom numbers: Try following code:

    int targetNumber; 
    targetNumber = new Random().nextInt(100) ;
Darshan Lila
  • 5,772
  • 2
  • 24
  • 34