Sha256: 58daa32020500b2f16e6dd5e9bb03d0630893dfb2810ea8949961a0da03bf19e

Contents?: true

Size: 619 Bytes

Versions: 2

Compression:

Stored size: 619 Bytes

Contents

require "rand_sum/version"

module RandSum
  class Error < StandardError; end

  def rand_sum(size)
    # examples 1
      # 400.rand_sum(2)
      # => [153, 247]
    # example 2
      # result = 400.rand_sum(2)
      # puts "#{result} sum => #{result.inject(:+)}"
      # => "[346, 54] sum => 400"
    array = Array.new()

    # add first random number
    array.push(rand(self))
    
    (1..(size - 2)).each do 
      array.push(rand(self - array.inject(:+)))
    end

    # add last number
    array.push(self - array.inject(:+)) if array.inject(:+) < 1000

    array
  end
end

class Integer
	include RandSum
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rand_sum-0.2.0 lib/rand_sum.rb
rand_sum-0.1.0 lib/rand_sum.rb