1
\$\begingroup\$

I have created a program to simulate a coin being tossed to land on heads/tails x amount of times in a row, where x can be defined by the user. The program then gets x amount of heads in a row, a fixed ten times. This is to generate an average.

using System;

namespace HeadsStreak
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter a head streak to get. This will be executed ten times to generate an average.");
            int desiredStreak = Convert.ToInt32(Console.ReadLine());
            int grandTotalAttempts = 0;
            int executionCount = 1;
            int currentStreak = 0;
            int totalAttempts = 0;
            Random rnd = new Random();
            do
            {
                while (currentStreak != desiredStreak)
                {
                    if (rnd.Next(0, 2) == 0)
                    {
                        currentStreak += 1;
                    }
                    else
                    {
                        currentStreak = 0;
                    }
                    totalAttempts += 1;
                }
                Console.WriteLine("Attempt Number: {0}", executionCount);
                Console.WriteLine("Total Attempts: {0}", totalAttempts);
                Console.WriteLine("");
                grandTotalAttempts += totalAttempts;
                executionCount += 1;
                currentStreak = 0;
                totalAttempts = 0;
            } while (executionCount <= 10);
            Console.WriteLine("Average Attempts: {0}", grandTotalAttempts/10);    
        }
    }
}

Let's say I ask for fifteen heads. The console may look something like this:

Enter a head streak to get. This will be executed five times to generate an average.
15
Attempt Number: 1
Total Attempts: 82761

Attempt Number: 2
Total Attempts: 42464

Attempt Number: 3
Total Attempts: 157531

Attempt Number: 4
Total Attempts: 79750

Attempt Number: 5
Total Attempts: 19214

Attempt Number: 6
Total Attempts: 4573

Attempt Number: 7
Total Attempts: 8161

Attempt Number: 8
Total Attempts: 99553

Attempt Number: 9
Total Attempts: 41036

Attempt Number: 10
Total Attempts: 84431

Average Attempts: 61947

This can execute fairly quickly within a reasonable timeframe. Time issues begin at 24, I assume this is because it takes 2^24 attempts to get 24 heads in a row. However, I am wondering if there is anything to improve in the code in order to improve performance so that higher streaks can be achieved within a reasonable timeframe?

\$\endgroup\$
1
\$\begingroup\$

Fails if they don't enter an integer
You are doing integer math on the average
I would prefer ++; over += 1;
I would rather see a foreach
Odd but rnd.Next(2) is faster
Need to get into the millions for a decent sample

public static void CoinToss()
{
    int sample = 1000000;
    Debug.WriteLine($"Enter a head streak to achieve. This will be executed {sample.ToString("N0")} times to generate an average.");
    int desiredStreak = 10;  
    int grandTotalAttempts = 0;
    int currentStreak;
    int totalAttempts;
    Random rnd = new Random();            
    for (int i = 0; i < sample; i++)
    {
        currentStreak = 0;
        totalAttempts = 0;
        while (currentStreak != desiredStreak)
        {
            if (rnd.Next(2) == 0)
            {
                currentStreak ++;
            }
            else
            {
                currentStreak = 0;
            }
            totalAttempts ++;
        }
        if ((i + 1) % (sample / 20) == 0)
        {
            Debug.WriteLine($"Attempt Number: {(i + 1).ToString("N0")}");
            Debug.WriteLine($"Total Attempts: {totalAttempts.ToString("N0")}");
            Debug.WriteLine("");
        }
        grandTotalAttempts += totalAttempts;  
    }
    Debug.WriteLine($"Average Attempts: {((decimal)grandTotalAttempts / sample).ToString("N2")}");
}
\$\endgroup\$
  • \$\begingroup\$ Use a foreach to iterate over an array with the attempts in it? \$\endgroup\$ – August Williams Jul 15 '17 at 15:03
  • \$\begingroup\$ No executionCount \$\endgroup\$ – paparazzo Jul 15 '17 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.