Generating three random booleans

Hello everyone,

So i am working on a simple game to play in the java console where you can choose between three cups This is my first program ive written myself. the only problem I am having is making sure two of the cups return false while one is true. Some ideas would be greatly appreciated ballIncup() assigns a true or false variable but it keep returning true when I test it out.

import java.util.Random;
import java.util.Scanner;

public class pickAcup {
	static Scanner input = new Scanner(System.in);
	static Random ball = new Random();
	static String name;
	static int turn = 1;
	static boolean cup1, cup2, cup3 = false;
	static String choice;
	
	public static void main(String[] args){
		intro();
		ballIncup();
		playerChoice();
	
	}
	
	 public static void intro(){
		// Introduction Takes in player name and welcomes them to the game
		System.out.println("Welcome to pick a cup please enter your name");
		name= input.nextLine();
		System.out.println("Hello " + name);
		
	}

	 public static void ballIncup(){
		 cup1 = ball.nextBoolean();
		 cup2 = ball.nextBoolean();
		 cup3 = ball.nextBoolean();
		 if(cup1 == false && cup2 == false){
			 cup3 = true;
		 }else if(cup1 == false && cup3 == false){
			 cup2 = true;
		 }else if(cup2 == false && cup3 == false){
			 cup1 = true;
		 }
	 }
	 
	 public static void playerChoice(){
		 System.out.println("Please pick cup 1, cup 2, or cup 3" + name);
		 choice = input.nextLine();
		 if(choice.equals("cup 1")){
			 if(cup1 = true){
				 System.out.println("You win!");
			 }else if(cup1 = false){
				 System.out.println("Please pick again");
			 }
			 
		 }
	 }
}

Well, since your learning how to code, I’ll bail you out of this one with a solution :slight_smile:

Instead of this…


 public static void ballIncup(){
       cup1 = ball.nextBoolean();
       cup2 = ball.nextBoolean();
       cup3 = ball.nextBoolean();
       if(cup1 == false && cup2 == false){
          cup3 = true;
       }else if(cup1 == false && cup3 == false){
          cup2 = true;
       }else if(cup2 == false && cup3 == false){
          cup1 = true;
       }
    }

Try this…


 public static void ballIncup(){
       int temp = ball.nextInt(3);
       cup1 = false;
       cup2 = false;
       cup3 = false;
       if(temp == 0){
          cup3 = true;
       }else if(temp == 1){
          cup2 = true;
       }else if(temp == 2){
          cup1 = true;
       }
    }

oh and one more thing…


public static void playerChoice(){
       System.out.println("Please pick cup 1, cup 2, or cup 3" + name);
       choice = input.nextLine();
       if(choice.equals("cup 1")){
-         if(cup1 = true){
+        if(cup1 == true){
             System.out.println("You win!");
-         }else if(cup1 = false){
+         }else if(cup1 == false){
             System.out.println("Please pick again");
          }
          
       }
    }

Be careful not to mix up the assignment operator (=) with the equality operator (==). The code is still, incomplete, but I’ll leave you to figure out the rest.

Pick a random number [0, int_max].


u = random...
v = random...
w = random..

a = int[] {u,v,w}
b = boolean[] {false, false, false}

m = min(u,min(v,w))
if (a[0] == m ) {b[0] = true};
if (a[1] == m) { b[1] = true};
if (a[2] == m) { b[2] = true};

Always gives you 1 true 2 false at random, unless you roll the same number at the same time, once in a blue moon.

No, there is even chance three of randoms produce same number ;D ctonmi231’s solution is the best.

Use this.


/**
 * Returns a random real number between 0 and x. The number is always
 * smaller than x.
 * 
 * @param x The maximum range
 * @return A random real number
 */
public static int random(int x){
    return (int) (Math.floor(Math.random() * x));
}

Then in code


boolean cup1 = (random(1)==0) ? true : false;
boolean cup2 = (random(1)==0) ? true : false;
boolean cup3 = (random(1)==0) ? true : false;

Here’s the Util class I use

https://code.google.com/p/game-engine-for-java/source/browse/src/com/gej/util/GUtil.java

How about;


double rnd=Math.random();
boolean b1=(rnd<=(1f/3));
boolean b2=(rnd>(1f/3) && rnd<=(2f/3));
boolean b3=(rnd>(2f/3));

Thank you guys so much for the help I got it working

Nextint isn’t magic, it can roll the same numbers 3 times in a row, it just asks for the next integer in the sequence that is built pseudo-randomly., the sequence can contain 3 same numbers in a row.

Funny thing is that humans tend to be terribly bad at judging what is random and what is not.

Which of the following dot distributions is random?

Most people guess the top one, but instead it is the bottom one. We’re just too good at recognizing patterns I guess. The pictures are from: this article, which is a nice read.

I picked bottom, whose the white area/free space’s deviation looks low.

Also this is a good example why sometimes we want to use the top “random” case because it will give better math properties on some problems. For example stochastic integration.

Now we have thoroughly confused the new guy… Our work is done.

How about this?


boolean bool = (System.nanoTime() & 0b1) == 0;

That should be pretty random, i think…

  • Longor1996

That is an awful random boolean. There is no guarantee on the resolution of the timer. They are never accurate to the nanosecond. So you have good odds that 2 calls in a row would give the same result. In fact that would almost always happen.

Seriously what is wrong with random.nextBoolean()?

The problem that is posted here is slightly different. There is 1 ball and 3 cups. This whole fiddling with booleans is extremely error prone and leads to many if-else chains/trees. This very thread is full of blatantly wrong suggestions.

What we need is:


    private final int cupCount = 3;
    private int ballFilledCupIndex = -1;

    public static void ballIncup(){
       ballFilledCupIndex = random.nextInt(cupCount);
    }
    
    public static void playerChoice(){
       System.out.println("Please pick cup 1, cup 2, or cup 3, " + name);
       choice = input.nextLine();
       if(choice.equals("cup "+(ballFilledCupIndex+1))){
          System.out.println("You win!");
       }
       else
          System.out.println("Please pick again");
       }
    }

and all boolean fields should be removed.

You all did it wrong. See how the lord did it? KNEEL!

riven is right of course. I should also at least read the first post.

Well there was nothing wrong with the answers per se. The difference is that everyone answered the question while Riven provided the solution for the problem.

O RLY…

Will lead to potentially multiple ‘true’ values.

Will lead to guaranteed multiple ‘true’ values, as random(1) always returns 0.

Horrible entrophy (zero bits), as explained by delt0r - and not answering any question.

This thread is better left unseen.

Whew, well the OP got it working way before they got down here, hopefully. I just hope other people would be able to figure out the right solution through trial and error :P…

I’m sorry, mr Riven, I didn’t mean the code. I meant the target of the answers.
On the code side, You are of course, right.