Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Generating three random booleans  (Read 3170 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #30 - Posted 2013-04-17 00:37:23 »

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? Smiley



Oh wait, did that sound condescending? persecutioncomplex Kiss

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #31 - Posted 2013-04-17 03:34:36 »

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 Smiley

Offline delt0r

JGO Knight


Medals: 27
Exp: 18 years


Computers can do that?


« Reply #32 - Posted 2013-04-17 07:47:41 »

Quote
I never said it was superior but everyone is dragging down everyone else besides Riven here.
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 have no special talents. I am only passionately curious.--Albert Einstein
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline brollysan

Junior Duke


Medals: 1



« Reply #33 - Posted 2013-04-17 21:48:10 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
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);
   }
}

1  
False counts: 0
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #34 - Posted 2013-04-17 22:11:04 »

truth be told I didn't bother to test my own answer earlier but it seems I wasn't wrong
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline brollysan

Junior Duke


Medals: 1



« Reply #35 - Posted 2013-04-17 22:22:28 »

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

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #36 - Posted 2013-04-17 22:59:05 »

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.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #37 - Posted 2013-04-17 23:49:56 »

but it is just as likely to break as your method
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #38 - Posted 2013-04-18 00:06:39 »

http://en.wikipedia.org/wiki/Pentium_FDIV_bug
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.

Myomyomyo.
Offline xsvenson
« Reply #39 - Posted 2013-04-19 21:52:02 »

Winner

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Pages: 1 [2]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!