Ad
  • Custom User Avatar
    1. Only in Ruby 2.4, and (at time of writing) Codewars uses Ruby 2.3.0
    2. Yep, this method will never be exact. The distribution will always sum to a power of two, which obviously can't ever be evenly divisible by 3. I probably can't really count this as a solution because it will only pass a not-very-stringent randomness test.
  • Custom User Avatar

    Very cool idea! Thanks.

    However, I have a couple of comments:

    1. reduce(:+) can be replaced by sum.
    2. I believe the resulting probability distribution is not precisely uniform - 1, 2 and 3 are produced in proportions 341 : 341 : 340. This can be shown by noticing that a sum of Bernoulli random variables follows a Binomial distribution. With your array size of 10 you therefore produce sums
      10, 11, 12, ... 20
      with (Binomial) proportions
      1 : 10, : 45 : 120 : 210 : 252 : 210 : 120 : 45 : 10 : 1
      therefore (%3 + 1) values 1, 2, 3 occur with proportions
      1+120+210+10 : 10+210+120+1 : 45+252+45
      i.e.
      341 : 341 : 342

    The deviation from uniformity is small, and gets smaller with larger array size.

  • Custom User Avatar

    Didn't think about using abs! Nice!