Split value in 24 randomly sized parts using C#

前端 未结 11 1096
忘了有多久
忘了有多久 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

    I tried the solutions from David and dahlbyk but had no luck. So here is what I came up with after reading the answer from mjv:

    public static class IntExtensions
    {
        public static IEnumerable Split(this int number, int parts)
        {
            var slots = Enumerable.Repeat(0, parts).ToList();
            var random = new Random();
    
            while (number > 0)
            {
                var slot = random.Next(0, parts);
                slots[slot]++;
                number--;
            }
            return slots;
        }
    }
    

提交回复
热议问题