-1

I want to create 7 stats for a character, randomly generating a value from 3-21, with the stat's sum being no higher than 91. I've tried arranging the stats into an array, and just going through them like this:

1) add random(15) to each array member
2) computing the total, subtracting from the 91 maximum
3) dividing this difference by 7
4) do step 1 with random(difference) adding it to the stat
5) Until I hit the 91 total.

Doing this a few hundred times I seem to get a curve where the 5,6, and 7th stats tend to be higher. And sometimes I hit the 4 or 5th stat and there are no more numbers to be added, meaning then that the first few stats get the most points. I think I am approaching this the wrong way to begin with. Any ideas? I have tunnel vision at this point I think.

4

2 回答 2

1

听起来你想多了。我可能会做这样的事情:

const
  STAT_QTY = 7;
  STATSUM_MAX = 91;
  STAT_MIN = 3;
  STAT_MAX = 21;

type
  TStatArray = Array [0..STAT_QTY-1] of integer;

然后在实施中:

function GenerateStats : TStatArray;
var statArr : TStatArray;
    i, statSum, excess, debit : integer;
    done : boolean;
begin
  Randomize;
  done := false;
  while not done do begin
    done := true;
    statSum := 0;
    for i := 0 to STAT_QTY - 1 do begin
      statArr[i] := STAT_MIN + Random(STAT_MAX - STAT_MIN);
      statSum := statSum + statArr[i];
    end;
    if statSum > STATSUM_MAX then begin
      excess := statSum - STATSUM_MAX;
      debit := excess div STAT_QTY + 1;
      for i := 0 to STAT_QTY -1 do begin
        statArr[i] := statArr[i] - debit;
      end;
    end;
    for i := 0 to STAT_QTY -1 do begin
      if statArr[i] < STAT_MIN then done := false;
    end;
  end;
  result := statArr;
end;

这会生成 3-21 范围内的随机统计数据列表。如果总和大于 91,则将超出部分除以统计数据的数量(使用div然后将答案四舍五入)并从每个数据中减去相等的数字。在极少数情况下,您最终的统计数据少于三个,请再做一次。任务完成。

测试了超过 2000 次迭代,我得到的平均统计数据为:

 [1] : 11.13893053  
 [2] : 11.15692154  
 [3] : 11.16141929  
 [4] : 11.11444278  
 [5] : 11.10194903  
 [6] : 10.9800100   
 [7] : 10.86856572

那是 11.07 的总平均值,标准偏差为 0.11——当然这是人们对具有您的构造参数的一般随机集合的期望。

于 2013-09-06T15:59:17.247 回答
0

这是一个稍微不同的方法的 C-ish 伪代码,假设有一个合适的random(N)函数返回 range 中的数字0 - N-1

int stats[7], deficit = 70;
for (int i = 0; i < 7; ++i)
  stats[i] = 3;               // initial assignments of the minimum to each stat
while (deficit)
{ int tmp = random(7);        // pick a random stat to bump
  if (stats[tmp] == 21)       // but not if it's already at max
    continue;
  ++stats[tmp];
  --deficit;
}

假设你random()是均匀分布的,那应该会产生很好的结果。

于 2013-09-06T20:43:12.313 回答