Split value in 24 randomly sized parts using C#

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

    class Numeric
      def n_rands(n)
        raw = (1..n).map { |x| rand } 
        raw.map { |x| x * to_f / raw.sum.to_f }.map { |x| x.to_i }.tap do |scaled|
          scaled[-1] = self - scaled[0..-2].sum
        end
      end
    end
    
    puts 1000.n_rands(10).inspect # [22, 70, 180, 192, 4, 121, 102, 179, 118, 12]
    

提交回复
热议问题