In: Computer Science
Using c# rewrite/edit the following program so that it runs the simulation 10,000 times (play the games 10,000 times) and count the number wins. Then calculate the winning probability by using the formula:
the expected winning probability = the number of wins / 10,000
using System;
class lottery
{
static void Main()
{
int n, random, choice = 1;
Random randnum = new Random();
while (choice == 1)
{
Console.Write("\nEnter a integer from 1 to 5:");
n = Convert.ToInt32(Console.ReadLine());
while (n < 1 || n > 5)
{
Console.Write("Invalid Input Enter again");
Console.Write("\nEnter a integer from 1 to 5:");
n = Convert.ToInt32(Console.ReadLine());
}
random = randnum.Next(1, 6);
if (n == random)
{
Console.WriteLine("You Won $5.");
}
else
{
Console.WriteLine("You lost $1");
}
Console.Write("1.To continue\n2.To exit\nEnter your
choice:");
choice = Convert.ToInt32(Console.ReadLine());
}
}
}
Please find the updated code and the output below.
using System;
class lottery
{
static void Main()
{
int n, random, count= 1 , number_of_win=0;
Random randnum = new Random();
while (count <= 10000)
{
random = randnum.Next(1, 6);
n = randnum.Next(1, 6);
if (n == random)
{
number_of_win++;
}
count ++;
}
Console.WriteLine("Number of win is = " + number_of_win);
Console.WriteLine("The expected winning probability is = "+
(number_of_win/10000.0));
}
}