示例#1
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest request, ILogger log)
        {
            if (request == null)
            {
                return(new BadRequestResult());
            }

            if (!request.Query.ContainsKey(NumberOfGamesParameter) || !int.TryParse(request.Query[NumberOfGamesParameter], out var numberOfGames) || numberOfGames <= 0)
            {
                return(new BadRequestObjectResult($"Query parameter {NumberOfGamesParameter} must be supplied and have an integer value greater than zero."));
            }

            if (!request.Query.ContainsKey(SwitchDoorParameter) || !bool.TryParse(request.Query[SwitchDoorParameter], out var switchDoor))
            {
                return(new BadRequestObjectResult($"Query parameter {SwitchDoorParameter} must be supplied and have a boolean value."));
            }

            log.LogInformation($"Azure Function \"Simulation\" performing simulation of {numberOfGames} games with strategy{(switchDoor ? " " : " do not ")}switch door.");

            var simulation = new InformedHostSimulation();
            var result     = simulation.Run(numberOfGames, switchDoor);
            var response   = new SimulationResponse(result);

            return(new JsonResult(response));
        }
示例#2
0
文件: Program.cs 项目: aschan/Monty
        internal static async Task SimulateNotSwitchingDoorsAsync(int numberOfGames)
        {
            var simulation = new InformedHostSimulation();
            var stopwatch  = Stopwatch.StartNew();
            var result     = await Task.Run(() => simulation.Run(numberOfGames, false)).ConfigureAwait(false);

            Console.WriteLine($"When not switching doors the contestant wins {result.WinPercentage} percent of the games.");
            stopwatch.Stop();
            Console.WriteLine($"Simulated {numberOfGames} games in {stopwatch.ElapsedMilliseconds} ms");
        }