CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2010
    Posts
    2

    [2008] List all possible sums of a combination

    I need some help with a math problem I'm trying to work out with visual basic.
    I need to calculate the probability of having a certain sum when randomly choosing 5 of 36 possible numbers.
    I already know all of the 36 possible numbers, and I know what the total number of possibilities is, I just need to write some code that will list all of the possible sums for me when you add any five of those thirty six numbers together. Ideally I would be able to also have it count how many of a certain sum occurred, but even if I could just have them as a list and I just count the instances, that would be fine too. Any help would be greatly appreciated, even if you could just give me a kick in the right direction.

  2. #2
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: [2008] List all possible sums of a combination

    Post some of the code you've written so far

  3. #3
    Join Date
    Nov 2010
    Posts
    30

    Re: [2008] List all possible sums of a combination

    This should work (tested it and it did for me)

    Code:
            Dim myList As New List(Of Integer)      'Contains the list of all numbers
            Dim A As UInteger = 1                   'Some counter variables
            Dim B As UInteger = 1
            Dim C As UInteger = 1
            Dim D As UInteger = 1
            Dim F As UInteger = 1
    
            While True                              'I break when the last variable (A) reaches > 36
                myList.Add(A + B + C + D + F)       'I just increment F till it gets bigger then 36
                F += 1                              'I then reset it to 1 and increment D with one
                If F > 36 Then                      'this is repeated for all untill finally A gets > 36
                    F = 1
                    D += 1
                End If
                If D > 36 Then
                    D = 1
                    C += 1
                End If
                If C > 36 Then
                    C = 1
                    B += 1
                End If
                If B > 36 Then
                    B = 1
                    A += 1
                End If
                If A > 36 Then
                    Exit While                      '<----Thats here
                End If
            End While
    
            Dim myHisto(180) As Integer             'An array to create a histogram, cinse
            Array.Clear(myHisto, 0, myHisto.Count)  'The maximum value = 180 (5 X 36) so I create one of 
            For Each entry As Integer In myList     'a length of 181 (Zero indexed!) I do this becouse it 
                myHisto(entry) += 1                 'makes filling easier and the index represents the 
            Next                                    'actual number (so not: 155 = actually the count of 156)
    
            Dim MaxCount As Integer = 0             'Some vars to store the max value and index of the histogram
            Dim MaxCountIndex As Integer = 0
    
            For Indexer As Integer = 0 To myHisto.Count - 1 'Loop through the histo printing the values and percentage chance
                'Replace the debug with something usefull. like a datagrid or console.print
                Debug.Print(String.Format("{0}{1}:{2}{3} = {4}&#37;", Indexer.ToString, vbTab, vbTab, myHisto(Indexer).ToString, CSng(myHisto(Indexer) / Math.Pow(36, 5) * 100)))
                If myHisto(Indexer) > MaxCount Then         'This filters the maximum count and 
                    MaxCount = myHisto(Indexer)             'stores it in maxCount
                    MaxCountIndex = Indexer                 'I also store the index which is the max count value
                End If
            Next
    
            Debug.Print(MaxCount.ToString & "   |   " & MaxCountIndex.ToString)    'And finally print it all
            'Please note that your example has two maximum thrown values, 92 and 93 I only show 92 here
            'I leave it to you to fix that
        End Sub
    Now please note that this is definitly not the prettiest answer but allowing for the KISS tradition
    (and just 15 minutes of work) this is the Simplest solution and therefor not prone to bugs and debugging is easy. The code
    can be a lot shorter using somthing like recusion.

    *Edit*
    btw: If you would Add the minimal number thrown to the maximum and devide it by 2 you also get the answer:
    Two D6:
    Minimum: 2
    Maximum: 12
    Total = 14
    14 / 2 = 7 (wattayouknow?)

    So:
    Min 5
    Max 180
    Total 185
    185 / 2 = 92.5 (mhhrrrm)
    AH! 92 and 93 (round down and up)
    This doesn't give you the total list of all thrown thoug :P

    (p.s. You got some funky dice :P)
    Last edited by CrashPilot; November 18th, 2010 at 07:59 AM.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [2008] List all possible sums of a combination

    That does not look right.
    Last edited by DataMiser; November 18th, 2010 at 09:44 AM.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Nov 2010
    Posts
    30

    Re: [2008] List all possible sums of a combination

    Quote Originally Posted by DataMiser View Post
    That does not look right.
    Looking wrong doesn't make it wrong untill you can deliver a reason why you believe it is wrong. For up to now this is a useless comment unless you are trying to farm posts then it would be usefull to you .

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [2008] List all possible sums of a combination

    Well for one thing I assume that 5 numbers from a possible 36 would mean 5 different numbers where the code posted starts by adding 1+1+1+1+1 does not seem to fit the question from the OP. Of course I could be wrong and that may be what he is looking for just not what I get from it.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [2008] List all possible sums of a combination

    If I am correct in assuming that you can not pick the same number 5 times then the code needs to be much different.

    I am thinking a set of nested for next loops with a > test in each to make sure that each combination of numbers is used only once and the same number does not appear twice in the same equation.
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Nov 2010
    Posts
    30

    Cool Re: [2008] List all possible sums of a combination

    I totally agree, if that were the case. I dont see him stating that each number must be unique just one of the 36 possibilities. I am not saying that I am right or wrong for that matter we'll have to wait for the author to clear that up.

    I personally thought this was a classic dice problem, you have 5 dice (D36), which number(s) would have the most chance of being thrown.

    I am sorry if you feel like I took a swing at ya, but I dislike answers as "You are wrong" or "I dissagree" and 99&#37; of the time those belong to postfarmers that like shiny titles :S.
    (clearly not you)
    Last edited by CrashPilot; November 19th, 2010 at 03:15 AM.

  9. #9
    Join Date
    Nov 2010
    Posts
    2

    Re: [2008] List all possible sums of a combination

    You could pick the same number five times, so that code is beautiful! Thanks a lot!

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [2008] List all possible sums of a combination

    List all possible sums of a combination
    Is a classic Factorial() problem. 5! = 1*2*3*4*5 = 120

    So, to resolve it, MARK IT RESOLVED using THREAD TOOLS!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured