伪随机加权数
本文关键字:加权 随机 | 更新日期: 2023-09-27 18:26:11
如果在t持续时间后未拾取,如何在I间隔生成对数字有偏差的n随机整数?
这段代码不会编译,但您可以了解我现有算法的要点。它可能不是特别有效,但它能完成任务,你必须根据自己的需求进行调整。即变量n
、timer
(创建定时器)、timerTicks
和方法ChooseRandomNumber
。
int maxNumber = n;
timer timer;
Dictionary<int, int> numbers
int timerTicks;
// Set up numbers with 0 base rating
SetUpDetails()
{
timer.interval = i
for (int i = 0; i < maxNumber; i++)
numbers.Add(i, 0);
}
timer.Tick
{
// List with numbers
List<int> numbersToChooseFrom;
foreach (KeyValuePair number in numbers)
{
// Must be added at least once
numbersToChooseFrom.Add(number.Key)
if (number.Value > timerTicks)
for (int i = 0; i < (number.Value/timerTicks); i++
// i.e. if number hasnt been chosen for
// timerTicks iterations give it more weight
numbersToChooseFrom.Add(number.Key)
// Some numbers occur more than once and thus have greater chance
int choice = ChooseRandomNumber(between 0 and numbersToChooseFrom.Length)
int RandomNumber = numbersToChooseFrom.ElementAt(choice)
return RandomNumber;
}
}