伪随机加权数

时间:2012-03-01 15:34:10

标签: c# algorithm math statistics

我如何在 i 间隔生成 n 随机整数,如果在 t 的持续时间后没有选择,则会对数字产生偏差时间?

1 个答案:

答案 0 :(得分:-1)

这段代码不会编译,但你得到了我所拥有的算法的要点。它可能不是特别有效,但它完成了工作,你将不得不根据自己的需要定制它。即变量ntimer(创建计时器),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;
  }
}