0

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"; } } } 
5
  • 2
    Put else between statements. Commented Dec 1, 2021 at 15:00
  • @RoyiNamir Fixed it. So the rest looks right? I can do it this way? What I'm thinking is, say rand = 96. Then the student won't be assigned to any classroom.. Commented Dec 1, 2021 at 15:02
  • 3
    So, your desired probabilities add up to 75%. What's meant to happen to the other 25% of students? That's a maths problem, not a programming one. Commented Dec 1, 2021 at 15:08
  • @Damien_The_Unbeliever I assume I shouldn't slump (0,100)? Commented Dec 1, 2021 at 15:10
  • In a real world, you should probably split on the count of students. Your algorithm (even if it's fixed) will put students with a certain probability into certain rooms. Since there's no guarantee that the random numbers will be evently distributed, you could still end up squeezing all into room Alpha. Commented Dec 1, 2021 at 15:12

1 Answer 1

5

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.