+ Reply to Thread
Results 1 to 3 of 3

Head Tail combinations

Hybrid View

  1. #1
    Registered User
    Join Date
    05-26-2014
    Posts
    3

    Head Tail combinations

    Hi,

    I need some help in coming up with the combination of coin tossing. When we throw a coin, we can have a two combinations, a Head and tail. so it can be HT. When we toss two coin , the combination HH,TH,HT,TT. When do toss three coins then it will be HHH, THH, HTH, TTH, HHT, THT, HTT,TTT.

    I would like to get a VBA program in excel which can generate the possible combinations of coins tossing, i need the output in a excel which will accept number of coins and the possible combinations. Its the program which can show the output of 2 to the power of n.

    can some one pls help.

    thanks
    vbax

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Head Tail combinations

    I think this might do what you want.
    Sub test()
        Dim HowManyCoins As Long
        Dim outArray() As String, outRange As Range
        Dim i As Long, Size As Long
        Set outRange = Sheet1.Range("q1")
        
        HowManyCoins = Application.InputBox("How many coins?", Default:=2, Type:=1)
        If HowManyCoins < 1 Then Exit Sub: Rem cancel pressed
        Size = 2 ^ (HowManyCoins)
        ReDim outArray(1 To Size, 1 To 1)
        
        For i = 1 To Size
            outArray(i, 1) = WorksheetFunction.Dec2Bin(i - 1, HowManyCoins)
            
            outArray(i, 1) = Replace(Replace(outArray(i, 1), "0", "H"), "1", "T")
        Next i
        
        With outRange.Resize(Size, 1)
            .EntireColumn.ClearContents
            .Value = outArray
        End With
    End Sub
    Last edited by mikerickson; 05-26-2014 at 07:10 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Head Tail combinations

    just in case you want more than 9
    Sub H_and_T()
    Dim list(), i&, x&, j&, c&, k&, n&
    n = InputBox("How many?")
    ReDim list(1 To 2 ^ n, 1 To 1)
    
    For i = 1 To n
        x = 2 ^ (n - i)
        For j = 1 To 2 ^ n Step x
            For k = j To j + x - 1
                list(k, 1) = list(k, 1) & Chr(72 + 12 * c)
            Next k
            c = c + 1: If c = 2 Then c = 0
        Next j
    Next i
    
    Cells(1, "c").Resize(2 ^ n, 1) = list
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. League Table Based on Head to Head results
    By darkblueblood in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-16-2023, 07:10 AM
  2. How to account for head to head matchup in Excel with other tie breakers
    By Biogeek1 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-26-2013, 05:18 PM
  3. Calculate average of last two outcomes of last two head-to-head clashes
    By wishkey in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-07-2013, 08:17 AM
  4. Ranking a league table by head to head results
    By pajc72 in forum Excel General
    Replies: 1
    Last Post: 09-02-2011, 07:02 AM
  5. How i can add description tail to cursor when it goes on to a spe.
    By Cell description tail in forum Excel General
    Replies: 2
    Last Post: 09-26-2005, 02:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1