lundi 2 novembre 2015

Random Number Generators in C# [duplicate]

This question already has an answer here:

Hey everyone I'm using C# to predict the results of a simulation of a whole school virus spread my school is doing (Just for the heck of it). I'm having problems with random number generation as the runtime speed is pretty darn fast and it's probably using the same seed everytime it iterates through the number cruncher loop. Is there a way to fix this? (Without adding a delay)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Human
    {
        public enum State
            {
                Healthy,
            Infected,
            Immune,
            Dead
            };
        public int infectionsLeft; //Constrainted inital value of 1-3
        public State state;

        public Human() //Constructor
        {
            state = State.Healthy; //Human is healthy
            Random randomizer = new Random();
            infectionsLeft = randomizer.Next(1, 4); //rand num from 1-3
        }
    }
    class Program
    {      
        static void Main(string[] args)
        {
            List<Human> humans = new List<Human>();
            double numberOfDays = 3; //Number of days simulation will be run
            double wastedCards = 0;
            for(int i = 0; i != 1000; i++)
            {
                Human human = new Human();
                if (i == 0)
                {
                    human.state = Human.State.Infected; //First human will start infected
                }
                humans.Add(human);
            }
            for (int a = 0; a != numberOfDays; a++)
            {
                foreach (Human human in humans)
                {
                    if (human.state == Human.State.Infected) //Infect if infected
                    {
                        Random r = new Random();
                        while (human.infectionsLeft > 0)
                        {                          
                            int theNextInfectedHuman = r.Next(humans.Count);
                            Human toBeInfectedHuman = new Human();
                            toBeInfectedHuman = humans[theNextInfectedHuman];
                            if (toBeInfectedHuman.state != Human.State.Healthy)
                            {
                                wastedCards++;
                            }
                            else
                            {
                                toBeInfectedHuman.state = Human.State.Infected;
                            }

                            human.infectionsLeft--;
                        }
                        int deathRoll = r.Next(1, 1001); //deathRoll! 33.33 percent chance of DEATH!
                        Console.WriteLine("DEATHROLL! " + deathRoll);
                        if(deathRoll < 234)
                        {
                            //SAFE!
                            human.state = Human.State.Immune;
                        }
                        else
                        {
                            //DEAD!
                            //Since this is a prediction of the simulation at school, I won't del the object,
                            //just set state to dead so cards can be wasted on a dead person too
                            human.state = Human.State.Dead;
                        }
                    }                  
                }
                //COUNT THE PPLS STATES!
                int healthy = 0;
                int infected = 0;
                int immune = 0;
                int dead = 0;
                foreach(Human human in humans)
                {
                    if(human.state == Human.State.Healthy)
                    {
                        healthy++;
                    }
                    else if (human.state == Human.State.Infected)
                    {
                        infected++;
                    }
                    else if (human.state == Human.State.Immune)
                    {
                        immune++;
                    }
                    else if (human.state == Human.State.Dead)
                    {
                        dead++;
                    }
                }
                using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"data.txt", true))
                {
                    file.WriteLine("-----DAY " + (a + 1) + "-----");
                    file.WriteLine("Healthy people: " + healthy);
                    file.WriteLine("Infected people: " + infected);
                    file.WriteLine("Immune people: " + immune);
                    file.WriteLine("Dead people: " + dead);
                    file.WriteLine("Wasted cards: " + wastedCards);
                }

            }
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire