Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to calculate in c# the possibility of this game

How to calculate in c# the possibility of this game

Scheduled Pinned Locked Moved C#
csharpgame-devtutorialquestion
30 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T trønderen

    Small hint: 80! is a number of about 120 decimal digits. An int can hold numbers up to roughly 9 digits.

    E Offline
    E Offline
    Exoskeletor
    wrote on last edited by
    #20

    that wasnt a hint!!! it appears to be the solution cause now this works

    using System.IO;
    using System;

    class Program
    {
    static void Main()
    {
    Console.WriteLine(CalculateProbabilityOfDrawNumbersFromAllNumbers(1,1));
    }
    private static double CalculateProbabilityOfDrawNumbersFromAllNumbers(double drawNumbers, double allNumbers)
    {
    return Factorial(allNumbers) / (Factorial(drawNumbers) * Factorial(allNumbers - drawNumbers)) * (Factorial(80 - allNumbers) / (Factorial(20 - drawNumbers) * Factorial(80 - allNumbers - (20 - drawNumbers)))) / (Factorial(80) / (Factorial(20) * Factorial(80 - 20)));
    }

        private static double Factorial(double x)
        {
            double f = 1;
            while (x > 1)
            {
                f \*= x--;
            }
            return f;
        }
    

    }

    im not sure why i was getting division by zero error. the int has surpass its largest number and return 0? i will use this crazy code to display it as a percentage without any unnecessary zeros in the end

        Console.WriteLine((CalculateProbabilityOfDrawNumbersFromAllNumbers(12,12) \* 100).ToString("F7").TrimEnd(new Char\[\] { '0' } ).TrimEnd(new Char\[\] { '.' } )+"%");
    
    L E 2 Replies Last reply
    0
    • E Exoskeletor

      that wasnt a hint!!! it appears to be the solution cause now this works

      using System.IO;
      using System;

      class Program
      {
      static void Main()
      {
      Console.WriteLine(CalculateProbabilityOfDrawNumbersFromAllNumbers(1,1));
      }
      private static double CalculateProbabilityOfDrawNumbersFromAllNumbers(double drawNumbers, double allNumbers)
      {
      return Factorial(allNumbers) / (Factorial(drawNumbers) * Factorial(allNumbers - drawNumbers)) * (Factorial(80 - allNumbers) / (Factorial(20 - drawNumbers) * Factorial(80 - allNumbers - (20 - drawNumbers)))) / (Factorial(80) / (Factorial(20) * Factorial(80 - 20)));
      }

          private static double Factorial(double x)
          {
              double f = 1;
              while (x > 1)
              {
                  f \*= x--;
              }
              return f;
          }
      

      }

      im not sure why i was getting division by zero error. the int has surpass its largest number and return 0? i will use this crazy code to display it as a percentage without any unnecessary zeros in the end

          Console.WriteLine((CalculateProbabilityOfDrawNumbersFromAllNumbers(12,12) \* 100).ToString("F7").TrimEnd(new Char\[\] { '0' } ).TrimEnd(new Char\[\] { '.' } )+"%");
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #21

      Quote:

      im not sure why i was getting division by zero error. the int has surpass its largest number and return 0?

      Don't just let that go, think, think until you figured it out!

      Luc Pattyn [My Articles] If you can't find it on YouTube try TikTok...

      1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        So what's the problem? that link leads you through the process of getting a probability from the inputs, so what have you tried to implement it? Where are you stuck? What help do you need? (Other than "somebody else to do all the work for me" which isn't the idea of this site at all.)

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        E Offline
        E Offline
        Exoskeletor
        wrote on last edited by
        #22

        can you say it 10 more times :P i think it will help

        1 Reply Last reply
        0
        • E Exoskeletor

          that wasnt a hint!!! it appears to be the solution cause now this works

          using System.IO;
          using System;

          class Program
          {
          static void Main()
          {
          Console.WriteLine(CalculateProbabilityOfDrawNumbersFromAllNumbers(1,1));
          }
          private static double CalculateProbabilityOfDrawNumbersFromAllNumbers(double drawNumbers, double allNumbers)
          {
          return Factorial(allNumbers) / (Factorial(drawNumbers) * Factorial(allNumbers - drawNumbers)) * (Factorial(80 - allNumbers) / (Factorial(20 - drawNumbers) * Factorial(80 - allNumbers - (20 - drawNumbers)))) / (Factorial(80) / (Factorial(20) * Factorial(80 - 20)));
          }

              private static double Factorial(double x)
              {
                  double f = 1;
                  while (x > 1)
                  {
                      f \*= x--;
                  }
                  return f;
              }
          

          }

          im not sure why i was getting division by zero error. the int has surpass its largest number and return 0? i will use this crazy code to display it as a percentage without any unnecessary zeros in the end

              Console.WriteLine((CalculateProbabilityOfDrawNumbersFromAllNumbers(12,12) \* 100).ToString("F7").TrimEnd(new Char\[\] { '0' } ).TrimEnd(new Char\[\] { '.' } )+"%");
          
          E Offline
          E Offline
          Exoskeletor
          wrote on last edited by
          #23

          im 99,99 percent sure that on overflow int will return 0 or -1 :P so this should be the issue

          L L 2 Replies Last reply
          0
          • E Exoskeletor

            im 99,99 percent sure that on overflow int will return 0 or -1 :P so this should be the issue

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #24

            It depends what it overflows from. For example if you add 1 to 2147483647 you will get -2147483648.

            E 1 Reply Last reply
            0
            • L Lost User

              It depends what it overflows from. For example if you add 1 to 2147483647 you will get -2147483648.

              E Offline
              E Offline
              Exoskeletor
              wrote on last edited by
              #25

              Wow it really works as the counters we have in cars that counts kilometers? They start all over again from the beginning, only for integers we also have negative ones

              L 1 Reply Last reply
              0
              • E Exoskeletor

                im 99,99 percent sure that on overflow int will return 0 or -1 :P so this should be the issue

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #26

                That is absolutely false. Your 99.99 percent isn't worth much, you haven't understood how integers typically work. Think again, and if necessary: experiment, observe, but stop guessing and "being sure". Here are three questions for you, first think about them; the answer is simple when you reason correctly. If you can't solve it, then try it, and search the explanation: 1. we all expect n! to be larger than (n-1)! and yet, with your original implementation this is not true for n=14, the value isn't larger, it is about 33% less!!! 2. multiplying positive numbers cannot ever yield a negative, and yet your code will claim 19! to be negative (no, not -1). 3. And finally, why is 34! the first one that returns zero... :)

                Luc Pattyn [My Articles] If you can't find it on YouTube try TikTok...

                E 1 Reply Last reply
                0
                • E Exoskeletor

                  Wow it really works as the counters we have in cars that counts kilometers? They start all over again from the beginning, only for integers we also have negative ones

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #27

                  Exoskeletor wrote:

                  Wow it really works as the counters we have in cars that counts kilometers?

                  Ehr.. no. Just look up how a PC stores numbers in binary, and the limitations of each number-"type".

                  Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                  E 1 Reply Last reply
                  0
                  • L Lost User

                    Exoskeletor wrote:

                    Wow it really works as the counters we have in cars that counts kilometers?

                    Ehr.. no. Just look up how a PC stores numbers in binary, and the limitations of each number-"type".

                    Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                    E Offline
                    E Offline
                    Exoskeletor
                    wrote on last edited by
                    #28

                    There was a time i new all those things in university but years have passed since, ok i have look them up

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      That is absolutely false. Your 99.99 percent isn't worth much, you haven't understood how integers typically work. Think again, and if necessary: experiment, observe, but stop guessing and "being sure". Here are three questions for you, first think about them; the answer is simple when you reason correctly. If you can't solve it, then try it, and search the explanation: 1. we all expect n! to be larger than (n-1)! and yet, with your original implementation this is not true for n=14, the value isn't larger, it is about 33% less!!! 2. multiplying positive numbers cannot ever yield a negative, and yet your code will claim 19! to be negative (no, not -1). 3. And finally, why is 34! the first one that returns zero... :)

                      Luc Pattyn [My Articles] If you can't find it on YouTube try TikTok...

                      E Offline
                      E Offline
                      Exoskeletor
                      wrote on last edited by
                      #29

                      Overflow could cause positive + positive to be negative and the other way around from what i have read.

                      T 1 Reply Last reply
                      0
                      • E Exoskeletor

                        Overflow could cause positive + positive to be negative and the other way around from what i have read.

                        T Offline
                        T Offline
                        trønderen
                        wrote on last edited by
                        #30

                        Certainly - and you would have an overflow exception as well. Unless you deliberately overlook it. If you don't specify any handling, but some default handler is called, it depends on this handler where execution continues. Maybe the storing of the (overflowed) result is skipped. If the sum variable was initially zero it may still be zero, if nothing is stored. Then: A factorial is never negative. So is it appropriate handle it as signed value at all? (You may still have an overflow, but if you ignore it, you have a true wraparound effect, like your car odometer)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups