我正在撰寫一個程式,我將不同的學生放在不同的教室。我在弄清楚如何在 C# 和一般編程中使用概率時遇到了一些麻煩。我希望成為 Alpha 學生的概率為 5%,Omega 為 10&,Nightwalkers 為 60%。我不明白要輸入什么數字。我現在的方法是:
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";
}
}
}
uj5u.com熱心網友回復:
您應該將ifs 中的數字相加:
if (rand < 5) {
student.Classroom = "Alpha";
}
else if (rand < 10 5)
{
student.Classroom = "Omega";
}
else if (rand < 60 10 5)
{
student.Classroom = "Nightwalkers";
}
請注意,這5, 10, 60是差異:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/371011.html
上一篇:.NET5類別庫xUnit測驗在比較AutoMapper中的物件時失敗
下一篇:Newtonsoft.Json.JsonReaderException:無效的JavaScript屬性識別符號字符:,
