+ Reply to Thread
Results 1 to 18 of 18

Coin Toss macro to show all possible outcomes of 10 flips.

  1. #1
    Registered User
    Join Date
    07-23-2014
    Location
    california, united states
    MS-Off Ver
    2010
    Posts
    2

    Coin Toss macro to show all possible outcomes of 10 flips.

    Hello Excel Forum,

    I'm a current computer science student with no experience in Excel's vba language and I've been given this task for an extra credit assignment for a non-cs related class.
    Sorry ahead of time if this kind of post is against the forum rules.

    I need to create a macro that shows all the possible outcomes of 10 coin flips (1,024) in the following format

    H H H H H H H H H H
    H H H H H H H H H T
    ....
    all the way through to

    T T T T T T T T T T


    This is my first time coding anything in excel vba and I have more experience with C++ and JavaScript and I'm not sure where to begin with tackling this problem.
    Any help would be appreciated in helping me get a good understanding of how to code this and how the code works.

    Thanks!

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    [Post Deleted]
    Last edited by JieJenn; 07-23-2014 at 06:28 PM. Reason: Solution was incorrect

  3. #3
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    In A1, enter =IF(MOD(INT( (ROW(F1012)-1) / (2^(COLUMN(F1012)-1)) ), 2)=1, "Head", "Tail"). Drag across 10 columns, and drag it down to row 1024
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    Quote Originally Posted by 2311ski View Post
    I've been given this task for an extra credit assignment for a non-cs related class. [....] I need to create a macro that shows all the possible outcomes of 10 coin flips (1,024) in the following format
    H H H H H H H H H H
    H H H H H H H H H T
    ....
    all the way through to
    T T T T T T T T T T

    This is my first time coding anything in excel vba and I have more experience with C++ and JavaScript and I'm not sure where to begin with tackling this problem.
    Since it is a class assignment -- and for extra credit -- I hope you understand that we should not provide turnkey solutions (i.e. the complete program). But we are happy to answer your how-to questions regarding the use of VBA.

    [Edit] Never mind! I see that JieJenn decided to deny you the opportunity to learn. If you are more interested in learning than getting credit for JieJenn's work, let us know how we can assist you further, perhaps by addressing my comments below.

    First, why don't you start by showig us how you would tackle the problem with C++ or JavaScript. Then perhaps we could help you "translate" that into VBA.

    Hint: Note that you can generate all head-tail sequences by incrementing a variable from 0 to 1023 and looking at its binary representation.

    Second, tell us what you don't know about "where to begin" with VBA.

    For example, do you know how to create VBA macros in general; or is that the first thing you would like to know? That's a fair question for us to answer.

    For another example, do you know the basics of VBA (no pun intended): how to declare a macro and variables; and how to write assignment statements and control statements (if, for, case, etc)? That's a lot to help you with. But we might point you to some online and offline resources.

    Finally, do you know how to put the results into a spreadsheet? That's a fair question for us to answer. But do you want the results in a spreadsheet; or do you need help in writing results into the Immediate Window or a text file?
    Last edited by joeu2004; 07-23-2014 at 07:02 PM.

  5. #5
    Registered User
    Join Date
    07-23-2014
    Location
    california, united states
    MS-Off Ver
    2010
    Posts
    2

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    Quote Originally Posted by joeu2004 View Post
    Since it is a class assignment -- and for extra credit -- I hope you understand that we should not provide turnkey solutions (i.e. the complete program). But we are happy to answer your how-to questions regarding the use of VBA.

    [Edit] Never mind! I see that JieJenn decided to deny you the opportunity to learn. If you are more interested in learning than getting credit for JieJenn's work, let us know how we can assist you further, perhaps by addressing my comments below.

    First, why don't you start by showig us how you would tackle the problem with C++ or JavaScript. Then perhaps we could help you "translate" that into VBA.

    Hint: Note that you can generate all head-tail sequences by incrementing a variable from 0 to 1023 and looking at its binary representation.

    Second, tell us what you don't know about "where to begin" with VBA.

    For example, do you know how to create VBA macros in general; or is that the first thing you would like to know? That's a fair question for us to answer.

    For another example, do you know the basics of VBA (no pun intended): how to declare a macro and variables; and how to write assignment statements and control statements (if, for, case, etc)? That's a lot to help you with. But we might point you to some online and offline resources.

    Finally, do you know how to put the results into a spreadsheet? That's a fair question for us to answer. But do you want the results in a spreadsheet; or do you need help in writing results into the Immediate Window or a text file?
    Thanks, that is exactly what I'm looking for, an understanding of the code and solution.

    First off if I was to code this in c++ I would use the following to generate a heads/tails result and after create an array of 1024 elements to hold the data to access and use that to put in a spreadsheet.
    Please Login or Register  to view this content.
    Where to begin with VBA, I personally have only used excel for minor school stuff and don't use it where I work and until a few days ago I was unaware there was a language for it.

    I've learned how to create a macro and assign variables from these pages:
    http://www.excel-easy.com/vba/create-a-macro.html
    http://www.excel-easy.com/vba/variables.html

    but that's as far as it goes. Also, I don't know how to put it in a spreadsheet and that's what I need it in.

    Thanks for all the help, I really appreciate it.

  6. #6
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    Quote Originally Posted by 2311ski View Post
    Thanks, that is exactly what I'm looking for, an understanding of the code and solution.
    Great!

    Quote Originally Posted by 2311ski View Post
    First off if I was to code this in c++ I would use the following to generate a heads/tails result and after create an array of 1024 elements to hold the data to access and use that to put in a spreadsheet.
    Please Login or Register  to view this content.
    Well, that would be wrong(!).

    According to you, the assignment is "create a macro that shows all the possible outcomes of 10 coin flips".

    Moreover, the assignment requires that the result be "in the following format", which appears to be from HHHHHHHHHH, HHHHHHHHHT to TTTTTTTTTT. I interpret that (perhaps incorrectly) to mean: in some reasonable order. (Do you agree with that interpretation?)

    Your C implementation would generate a random sample of 10 coin flips. That does not guarantee you will generate all 1024 combinations; in fact, statistics tells us there is a high probability you will not. And it does not guarantee the combinations are generated in "some reasonable order".

    Instead, the C implementation is much simpler. Think of H as 0 and T as 1. Then HHHHHHHHHH is the binary value 0000000000; HHHHHHHHHT is the binary value 0000000001; and TTTTTTTTTT is the binary value 1111111111.

    As a computer science student, if you have been introduced to binary representation, that should suggest a novel way to represent all of the combinations of 10 ones and zeros -- I mean H and T. ;-) Are you familiar with binary representation?

    (Actually, I already gave you that much as a "hint" in my previous posting.)

    Quote Originally Posted by 2311ski View Post
    I've learned how to create a macro and assign variables from these pages:
    http://www.excel-easy.com/vba/create-a-macro.html
    http://www.excel-easy.com/vba/variables.html
    but that's as far as it goes. Also, I don't know how to put it in a spreadsheet and that's what I need it in.
    Great! You are a good self-starter.

    Also take a look at http://www.excel-easy.com/vba/loop.html.

    But as you realize, that set of files is seriously lacking in sufficient detail.

    Once you present a credible C implementation, we can help you translate it into VBA.

    (Let us know if you are totally stuck.)

    PS.... Previously, you wrote this is for a non-CS class. What class is it for? Are you really expected to learn VBA on your own?
    Last edited by joeu2004; 07-24-2014 at 12:44 AM. Reason: cosmetic

  7. #7
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    PS....
    Quote Originally Posted by joeu2004 View Post
    Once you present a credible C implementation, we can help you translate it into VBA.
    Sorry, I got a little pedantic. The following might help you get started with VBA, if your C implementation is anything like mine. ;-)

    C VBA
    statement; each statement on a separate line;
    no semicolon terminator
    // comment ' comment
    int i; dim i as integer ' or long (preferred)
    char s; dim s as string*1
    for (i = 0; i <= 1023; i++) {...} for i = 0 to 1023 ' step 1 (implied)
    ...
    next i
    for (j = 10; j >=1; j--) {...} for j = 10 to 1 step -1
    ...
    next j
    x /= 2; x = x \ 2
    s = (i & 1) ? 'T' : 'H'; s = iif(i AND 1, "T", "H")
    cells(i,j)=s ' store into Excel row i, column j
    if (expr) {...}
    else {...}
    if expr then ' see if-then-else help page for more
    ...
    else
    ...
    end if
    Last edited by joeu2004; 07-24-2014 at 06:47 PM. Reason: errata: flipped "T" and "H" b/c T=1, H=0

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    In any language, I would think the simplest way would be to nest 10 loops, each one having a value of 0 or 1.

    Use the value of the loop counter to index an array containing "H" and "T" as its elements.

    At the inner level, concatenate the 10 values into a result variable, increment a row counter and output the result to column A indexed by the row counter.

    The values from HHHHHHHHHH to TTTTTTTTTTTT would be in single cells

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    Just thought: instead of indexing the array, you could just concatenate the values and then replace "0" with "H" and "1" with "T"

    Doesn't make it shorter, just doesn't need the array.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    @JieJenn: you confused me a little.

    In your post #3, the formula should be:

    A1:
    Formula: copy to clipboard
    Please Login or Register  to view this content.



    It is correct (obviously) in the workbook but a random cell formula has been copied into the post. Wondered why I couldn't understand the output

    Regards, TMS

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

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    here's VBA code for OP problem, 10 H and T
    this uses 3 nested loops, but same result can be done with only 2 if wanted
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    11-18-2014
    Location
    Chicago,IL
    MS-Off Ver
    2013
    Posts
    1

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    I have a similar problem. I am good with C++, but no idea how to use VBA. I have the logic down, and I want to use this code as my framework. How would I go about making it from 10 flips, to N flips?

    Thanks

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

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    Are you referring to any particular contribution to this thread?

    For post#11 just change the second line to Const n& = 6, or whatever your N (flips) is.

    Although for large enough N you may run into either memory limits of your computer or worksheet size limitation.

    The latter is easy enough to overcome by overflowing into another column or another worksheet.

    The memory limits problem would need either a more efficient code if you can find one or a more powerful computer.

  14. #14
    Registered User
    Join Date
    12-29-2017
    Location
    York, Pennsylvania
    MS-Off Ver
    2013
    Posts
    2

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    Is this any faster than two for loops?

  15. #15
    Registered User
    Join Date
    12-29-2017
    Location
    York, Pennsylvania
    MS-Off Ver
    2013
    Posts
    2

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    Is using 3 for loops faster than using 3 for loops?

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

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    Referring only to post#11, that code uses 3 nested loops but modifying it to 2 nested loops at the cost of a bit more complication elsewhere makes it a bit slower than the 3 with a bit less complication elsewhere.
    However, that's how it goes.

  17. #17
    Registered User
    Join Date
    05-16-2018
    Location
    London,England
    MS-Off Ver
    2016
    Posts
    1

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    Hi, thanks for the working sheet. Would you be able to do a sheet for 7 coins please? I tried and failed with your instructions. I know its 128 possible out comes but how to do it I'm struggling. Thanks

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

    Re: Coin Toss macro to show all possible outcomes of 10 flips.

    If it's post#11 you refer to, change the second line to Const n& = 7 and then just run the code.

+ 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. Generating a Random Heads or Tails Result Over X Coin Flips
    By COgreywolf in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-19-2012, 04:18 PM
  2. Modifiy Coin Simulation
    By mmart001 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-10-2011, 06:52 PM
  3. BreakThrough In Coin Toss System
    By scalpmaster in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-22-2010, 03:40 PM
  4. Replies: 1
    Last Post: 02-20-2006, 05:50 PM
  5. [SOLVED] Multiple coin flips in a single cell
    By igor eduardo küpfer in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-13-2005, 11:05 AM

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