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.

publicstaticvoidintro(){// Introduction Takes in player name and welcomes them to the gameSystem.out.println("Welcome to pick a cup please enter your name");name= input.nextLine();System.out.println("Hello " + name);

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()?

I have no special talents. I am only passionately curious.--Albert Einstein

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.

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.

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:

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))

I have no special talents. I am only passionately curious.--Albert Einstein

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:

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.

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.

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.

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.

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:

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...

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org