Generating three random booleans

Actually Riven provide solution on smaller context, the game, while rest of you provide a learn how to make 1 true 2 false which MAYBE he will need on his later job programming rocket controller or whatever. It’s about giving fish or teaching to fish.

How was my suggestion wrong?

Like Riven said, there is a chance that there will be multiple true values, when the OP specifically requested 1 true and 2 false. You can’t exactly have 2 cups that are “true”, that’s not how the game works. Let’s look at what you posted:

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};

If two of the numbers are the same, there will be 2 true values. If 3, then 3 true values. -other stuff edited out due to math problems-

The math is wrong. You would get duplicates a little bit more than 1% of the time. Given that you have a duplicate it will be the smallest number just a little bit more than half the time. So a total error of more than one true of a little over .5%

The little over are for 3 way ties.

And still this thread refuses to die… Its a Zombie. Eating unsuspecting brains for breakfast.

[edit] This is assuming as in the previous post that the ints are chosen between 0 and 99 (aka nextInt(100))

The likelihood of two numbers being the same is a factor of 1/Integer.MAX_VALUE. This is no different than rivens suggestion in terms of getting the same number twice. NextInt is not magic.

Wrong…try again.

It can fail so it’s not a good solution. Unlikely != impossible. Try doing some multithreading.

Seriously, how is that code superior to simply


Random random = new Random();

...

Arrays.fill(a, false); //Clear to false
a[random.nextInt(3)] = true; //Set one random element to true

I never said it was superior but everyone is dragging down everyone else besides Riven here. Granted he is well-versed in Java (I have seen some of his posts, especially the green-thread supports this claim) his answer is just as fallible as ours (mine) even if the code for it is shorter (because of not converting to booleans). He is using the java library, so am I , his NextInt call will be just as likely to reproduce the same number twice as mine is.

But my mistake for expecting polite and reasonable behavior from the members here, I am too used with sites like SO. Instead of constructive criticism (like pointing out why my answer is wrong, providing proof for it failing 1% of the time) that would better the community we turn to condescension.

I think there is a misunderstanding here. It isn’t about whether random.nextInt(n) returns the same number twice, it’s perfectly fine if 1 ball is under the same cup in a few games in a row. It’s not fine when the program tells you that the ball is in cup 1 and 3 in the very same game.

[quote=“brollysan,post:28,topic:41487”]
The difference is that the way your code calls nextInt(…) results in the possibility that two or three booleans fields are set to true, which is an incorrect state. My code will always leave exactly 1 ball in 3 cups, all the time, without any chance of an incorrect state. That we’re both using nextInt(n) and your implementation potentially causes the program to reach an invalid state, doesn’t mean that my code suffers from the same flaw.

[quote=“brollysan,post:28,topic:41487”]
Sigh. Oh well, here’s is the proof, not by obscure reasoning, but simply by bruteforcing your algorithm, and counting the number of times it reaches an invalid state:


			Random r = new Random(123L);

			int[] ballHistogram = new int[4];

			for (int i = 0; i < 1000 * 1000; i++) {
				int u = r.nextInt(3);
				int v = r.nextInt(3);
				int w = r.nextInt(3);

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

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

				// verify internal state
				int ballCount = 0;
				if (b[0]) {
					ballCount++;
				}
				if (b[1]) {
					ballCount++;
				}
				if (b[2]) {
					ballCount++;
				}
				ballHistogram[ballCount]++;
			}

			System.out.println("total games: " + (ballHistogram[0] + ballHistogram[1] + ballHistogram[2] + ballHistogram[3]));
			for (int i = 0; i < ballHistogram.length; i++) {
				System.out.println("games with " + i + " balls in 3 cups: " + ballHistogram[i]);
			}

Output:


total games: 1000000
games with 0 balls in 3 cups: 0
games with 1 balls in 3 cups: 555142
games with 2 balls in 3 cups: 333727
games with 3 balls in 3 cups: 111131

In short: over 44% of the games end up in an invalid state, a far cry from 1/Integer.MAX_VALUE, or the proposed 0.5% and 1.0%.

I hope this elaborate explaination brings JGO back to ‘StackOverflow quality’ :-*

Sigh. The sad part is that this whole thread could have been avoided if someone had just reformulated the question from “I need to set 1 boolean to true and 2 to false randomly” to “I need to place a ball in a random cup” instead…

When answering questions, always disregard every assumption and partial solution the author has made. I mean, if he knew what he was doing, he wouldn’t be posting topics, right? Right? :slight_smile:

Oh wait, did that sound condescending? :persecutioncomplex: :-*

Can I just jump in with my confusion as to why booleans are necessary in the first place? Can’t you just store the number of the cup that is filled in a simple integer variable…?

In either case, this thread is hilarious :slight_smile:

[quote]I never said it was superior but everyone is dragging down everyone else besides Riven here.
[/quote]
I have been programming for many years, and i did it in this thread too. I didn’t read the first post properly.

There are times where you just have to suck it up because you really did write stupid, brain dead code. You become a better programmer when you can learn from criticism. This is even more true when your starting out.

Riven even does it sometimes :persecutioncomplex:

I am not sure your code is right riven. You are checking if ANY of them is true, which is not the OPs requirement, he requires that ONE be true at most. The (I think proper) test returns zero false positives out of a billion. And thank you for replying with a proper explanation, truth be told I didn’t bother to test my own answer earlier but it seems I wasn’t wrong. Neither were you in your answer, I just got frustrated reading some of the childish replies here.


public class TwoBallsOneCup {
	public static void main(String[] args) {
		int falseCount = 0;
		int loops = (int) 1E+9;
		for (int i = 0; i < loops; i++) {

			int u = (int) (Integer.MAX_VALUE * Math.random());
			int v = (int) (Integer.MAX_VALUE * Math.random());
			int w = (int) (Integer.MAX_VALUE * Math.random());
			int m = Math.min(u, Math.min(v, w));

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

			// Verify, we require ONE true, not if any is true. See rivens

			if (b[0]) {
				// One true, are others true?
				if (b[1] || b[2]) {
					falseCount++;
					System.out.println("Failed, two trues");
				}
			}

			if (b[1]) {
				if (b[0] || b[2]) {
					falseCount++;
					System.out.println("Failed, two trues");
				}
			}

			if (b[2]) {
				if (b[0] || b[1]) {
					falseCount++;
					System.out.println("Failed, two trues");
				}
			}
		}
		System.out.println("False counts: " + falseCount);
	}
}


False counts: 0

[quote=“brollysan,post:34,topic:41487”]
Your algorithm is wrong. It’s simple as that. It’s not tolerated in any business if your logic can break at any time. That it only breaks (on average) once in 2 billion times, doesn’t mean that it won’t crash in the first dozen runs. It’s even worse that it’s a silent error, as when it fails, you may not even notice - you just give the player an unfair advantage of finding more than 1 ball in 3 cups.

Well truthfully it only breaks once in 3/Integer.Max_Value times. The loop has to run about 2^30 times before it has a 10% chance to break. But I see your point, it is bad practice to trust the coin-flip (however unlikely it is to break) but it is just as likely to break as your method and I simply wanted to give the OP a tip on how to do it, I didn’t want to write the code for him. Just on principle I don’t like to be treated unfairly or on wrong assumptions nor do I like it when people are rude (not necessarily implying that you were or that anyone was to me or that I feel offended). Half this thread is full of my posts complaining! If only everyone was being helpful and constructive.

This is still ongoing… There is only one question that needs to be answered.

“Can this solution ever be wrong?”

Riven’s solution is never going to produce an error, because it guarantees that only one ball will be in a cup at a time. However, your solution can allow any of up to 3 balls at a time randomly. Regardless of the chances of error, this solution is not going to be 100% guaranteed.

Just like a user wouldn’t want a calculator to produce 2+2=5 one percent of the time, neither would any programmer want a solution to only work randomly. Always strive for 100% accuracy, we should at least try to be as consistent as a calculator when programming.

I’m sorry that you don’t see how my approach will never break. Given that I can’t convince you, I’ll just stop trying, before others, like the always appreciated ctomni231, get annoyed by this chitchat.


A floating point division error which only occurs in 1 out of every 9 billion divisions resulted in Intel being forced to allow people to get their processors replaced with fixed ones. Total cost: $450 million.

Winner