Non-Uniform Random Number Generator Implementation

2019-02-13 21:31发布

I need a random number generator that picks numbers over a specified range with a programmable mean.

For example, I need to pick numbers between 2 and 14 and I need the average of the random numbers to be 5.

I use random number generators a lot. Usually I just need a uniform distribution.

I don't even know what to call this type of distribution.

Thank you for any assistance or insight you can provide.

8条回答
乱世女痞
2楼-- · 2019-02-13 21:59

My first idea would be:

  • generate numbers in the range 0..1
  • scale to the range -9..9 ( x-0.5; x*18)
  • shift range by 5 -> -4 .. 14 (add 5)
  • truncate the range to 2..14 (discard numbers < 2)

that should give you numbers in the range you want.

查看更多
你好瞎i
3楼-- · 2019-02-13 22:00

You need a distributed / weighted random number generator. Here's a reference to get you started.

查看更多
登录 后发表回答