我有一个骰子滚动游戏,我正在使用以下逻辑。我希望从6到12的骰子值应该以较小的概率下降。根据骰子值,学生奖励积分增加。骰子值越高,奖励积分越高。因此骰子值从2到5将频繁下降,骰子值从6到12应该只下降到每100名学生。这是一个Windows应用程序,每次掷骰子并且用户获得骰子值时,应用程序关闭并再次打开以供另一个用户掷骰子。那么如何追踪第100名学生的骰子价值在6到12之间。
for (Int32 i = 0; i < numberOfDice; i++)
{
Int32 roll = rnd.Next(1, numberOfSides);
total += roll;
result.AppendFormat("Congrats..!!! You got Dice {0:00}:\t{1}\n", i + 1, roll);
}
请帮忙。 关于如何做到这一点的任何想法。请帮忙。
答案 0 :(得分:2)
var sweeps = new List<int>();
for (int i = 1; i < 6; i++)
{
for (int j = 0; j < 100; j++)
{
sweeps.Add(i);
}
}
for (int i = 6; i <= 12; i++)
{
sweeps.Add(i);
}
var count = sweeps.Count;
for (int i = 0; i < numberOfDice; i++)
{
int roll = sweeps[rnd.Next(1, count)];
total += roll;
result.AppendFormat("Congrats..!!! You got Dice {0:00}:\t{1}\n", i + 1, roll);
}
答案 1 :(得分:0)
public int RollLoadedDie(int numberOfSides)
{
const lesserProbability = 50;
int ret;
bool keepValue;
do
{
ret = rnd.Next(1, numberOfSides);
if(ret == 6 || ret == 12)
keepValue = rnd.Next(1, 100) > lesserProbability;
else
keepValue = true;
}
while(keepValue == false);
return ret;
}
答案 2 :(得分:0)
public int RollDice(int numDice, int numSides)
{
int total = 0;
for (int i = 0; i < numDice; i++)
{
int roll;
if(rnd.NextDouble() < 0.01)
roll = rnd.Next(6, numSides + 1);
else
roll = rnd.Next(1, 5 + 1);
total += roll;
result.AppendFormat("Congrats..!!! You got Dice {0:00}:\t{1}\n", i + 1, roll);
}
return total;
}
此代码将每100次粗略地生成一个从6到maxSides的值。如果你想要它只是6-12,你可以试试这个。
public int RollDice(int numDice, int numSides)
{
int total = 0;
for (int i = 0; i < numDice; i++)
{
int roll;
if(rnd.NextDouble() < 0.01)
roll = rnd.Next(6, Math.Min(12, numSides) + 1);
else
{
roll = rnd.Next(1, numSides - (Math.Min(12, numSides) - 6 + 1) + 1);
if(roll >= 6)
roll += (Math.Min(12, numSides) - 6 + 1);
}
total += roll;
result.AppendFormat("Congrats..!!! You got Dice {0:00}:\t{1}\n", i + 1, roll);
}
return total;
}
答案 3 :(得分:0)
public int roleDice(int numberOfSides)
{
// the chance in which the dice throws a value from 6 to 12
int Large = rnd.Next(1,100) ;
for(int i =1 ; i < 101 ; i++)
{
if(i ==Large) // Only once for 100 times we throw a value between 7 , 12 included.
keepValue = rnd.Next(7,12);
else // For rest throw a value between 1 and 6
keepValue = rnd.Next(1,6);
}
return keepValue
}
答案 4 :(得分:0)
Random
生成器是统一的。因此,使用三个RNG:一个用于决定是生成大值还是小值,两个用于分别生成小值和大值。
var decider = new Random();
var largeRng = new Random();
var smallRng = new Random();
var deciderValue = decider.Next(1, 100);
int generatedValue;
if (deciderValue == 1) // will happen with probability 1/100
{
generatedValue = largeRng.Next(6, 12);
}
else
{
generatedValue = smallRng.Next(1, 5);
}
PS:如果您使用相同的生成器来决定要生成哪些值,然后再次使用它来生成这些值,就像某些答案一样,生成的小值和大值将 NOT 均匀分布。