Get `n` random values between 2 numbers having average `x`

前端 未结 5 636
你的背包
你的背包 2021-01-28 12:21

I want to get n random numbers(e.g n=16)(whole numbers) between 1 to 5(including both) so that average is x.

x can be any value between (1, 1.5, 2, 2.5, 3,

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-28 12:40

    I would implement it like this:

    1. Choose n random numbers
    2. Calculate average
    3. Randomly choose one of the n random numbers
    4. Add or subtract 1 from the number, depending on whether the current average is above or below x
    5. Repeat from step 2 until the current average is x (or close enough)

提交回复
热议问题