Split value in 24 randomly sized parts using C#

前端 未结 11 1074
忘了有多久
忘了有多久 2021-02-03 12:01

I have a value, say 20010. I want to randomly divide this value over 24 hours. So basically split the value into a 24 slot big array where all slots are randomly big.

Wh

11条回答
  •  后悔当初
    2021-02-03 12:17

    Assuming that you don't want to have much (any) control over the distribution of sizes, here's an approach that would work (pseudo-code).

    1. Create a list of 24 random values, generated however you like, at whatever scale
    2. Find the sum of this list
    3. Create your final list by scaling the 24 random values against your total

    Notes

    • If you use floating point arithmetic, you may be off by one or two. To avoid this, don't use scaling to complete the last value, instead fill it in with the total remaining.
    • If you do need tighter control over the distribution, use a different method to generate your initial array, but the rest doesn't need to change.

提交回复
热议问题