CoinToss | Code Example

Coding always fun ! Here is a C# code example of CoinToss which you can run using MsTest.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace HackerRank
{
    [TestClass]
    public class Day03
    {
        public string TossACoin()
        {
            Random rand = new Random();
            int toss = Math.Abs(rand.Next()) % 2;
            if (toss == 0)
            {
                return "HEADS";
            } else
            {
                return "TAILS";
            }
        }

        [TestMethod]
        public void CoinToss()
        {
            Day03 game = new Day03();
            Console.WriteLine(game.TossACoin());
            Console.WriteLine(game.TossACoin());
            Console.WriteLine(game.TossACoin());
            Console.WriteLine(game.TossACoin());
            Console.WriteLine(game.TossACoin());
            Console.WriteLine(game.TossACoin());
        }
    }
}
Advertisements

Leave a Reply