Vincent
Vincent

Reputation: 9

Java Random Number generator generate same number but when I compare them it is not same

I am very new to Java Programming. For example, even if I roll the same number i still lose the bet. If I roll like one and fine, I still win the bet amount. I am trying to fix that problem for hours. But can't figure it out. Please, someone, help me. Thanks in advance. Here is my code.

public class Dice {
    private int dice;
    public Random number;

    //default constructor
    public Dice() {
        number = new Random();
    }
    //To generate random number between 1 to 6. random starts from 0. there is no 0 on dice. 
    //By adding one, it will start at 1 and end at 6

    }

    //Method to check two dice
    public boolean isequal(int dice1,int dice2) {

        }
        else
}

public class Playgame
{
 //
    }

    public static void main(String[] args) {
     //
            }
        }

        {
            return false;
        }
    }
        userinput.close();
    }
}

Upvotes: 1

Views: 445

Answers (4)

TaQuangTu
TaQuangTu

Reputation: 2343

Each time you call ob1.play() method, it will give you different numbers.

in if clause:

 if(obj1.isequal(obj1.play(), obj1.play()) == true)

will give you two random values that different from two random values in if block:

System.out.println("You rolled a " + toString(obj1.play()) + " and a " + toString(obj1.play()) );

Upvotes: 0

Gokul raj
Gokul raj

Reputation: 53

no need for the dice class if it is to generate the random number and checks whether two number is equal or not. try the code below it will work

Random random = new Random();
int n1 = random.nextInt(6) + 1;
int n2 = random.nextInt(6) + 1;
System.out.println("You rolled a " + toString(n1)+ " and a " +toString(n2));
if (n1 == n2) {
    double win = betmoney * 2;
    System.out.println("You win $" + win);
    startmoney += win;
} else {
    startmoney -= betmoney;
    System.out.println("You lose $" + betmoney);
    System.out.println("You left only $" + startmoney);
}

problem with your code is your generating random numbers two times 1.during condition check and 2. in the sysout statement. your program is working fine only. but due to this your confusing yourself that it.

Upvotes: 0

Michael
Michael

Reputation: 44150

At least one problem is here (there may be others) :

if(obj1.isequal(obj1.play(), obj1.play()) == true)
{
    System.out.println("You rolled a " + toString(obj1.play()) + " and a "
        + toString(obj1.play()) );

When you print the message, you are calling obj1.play() again and generating 2 new random numbers. If you need to use the value twice (once for comparison and once for printing) then you should store it in a variable.

int firstRoll = obj1.play();
int secondRoll = obj1.play();
if(obj1.isequal(firstRoll, secondRoll) == true)
{
    System.out.println("You rolled a " + toString(firstRoll) + " and a "
      + toString(secondRoll) );

    //...

Upvotes: 2

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

Each call to obj1.play() return a different values.

Hence your test: obj1.isEqual(obj1.play(), obj1.play()) will mostly not return true.

Upvotes: 1

Related Questions