Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SeatProblem
- {
- class Program
- {
- //entry point
- public static void Main()
- {
- Random rng = new Random(); //Create a RNG
- const int n = 10000000; //number of trials
- int lastSeatAvailableCounter = 0; //counter that we will increase each time the last seat is available
- //loop N times
- for (int trials = 0; trials < n; trials++)
- {
- int[] seats = new int[100]; //array of 100 seats
- //fill the seats array with 0
- for (int i = 0; i < 100; i++)
- seats[i] = 0;
- //pick a random seat for first person
- int firstSeat = rng.Next(0,100);
- seats[firstSeat] = 1;
- //loop to seat people 2-100
- int currentPerson = 2;
- while (currentPerson <= 99)
- {
- int seatNumber = currentPerson - 1;
- while (seats[seatNumber] != 0)
- seatNumber = rng.Next(0,100); //pick a random number from 0-100 (not inclusive of 100)
- seats[seatNumber] = currentPerson;
- currentPerson++;
- }
- //if last seat is available, increment counter by 1
- if (seats[99] == 0)
- lastSeatAvailableCounter++;
- }
- double result = (double)lastSeatAvailableCounter / (double)n;
- Console.WriteLine(lastSeatAvailableCounter);
- Console.WriteLine(result);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement