Home > Enterprise >  C# Different probabilities in if statements
C# Different probabilities in if statements

Time:12-02

I am writing a program where I put different students in different classrooms. I'm having some trouble figuring out how to use probabilities in C#, and programming in general. I want it to be a 5% probability to become a student in Alpha, 10& for Omega & 60% for Nightwalkers. I don't understand what numbers to put in.My method right now:

    public string AssignClassroom()
    {
        int rand = random.Next(0, 100);
        {
            if (rand < 5) // or >95%?
            {
                student.Classroom = "Alpha";
            }

            if (rand < 10) // or >90?
            {
                student.Classroom = "Omega";
            }

            if (rand < 60) // or >40?
            {
                student.Classroom = "Nightwalkers";
            }
        }
    }

CodePudding user response:

You should add up figures in ifs:

        if (rand < 5)             {
            student.Classroom = "Alpha";
        }
        else if (rand < 10   5) 
        {
            student.Classroom = "Omega";
        }
        else if (rand < 60   10   5) 
        {
            student.Classroom = "Nightwalkers";
        }

Note, that 5, 10, 60 are differencies:

  0  .. 5  .. 15 .. 75
  |  5  |     |     |  ->  5% top students go to the 1st class 
        | 10  |     |  -> 10% next go to the 2nd 
              | 60  |  -> 60% goes to the 3d
  • Related