Sha256: 6e4a244b8c6220eb288e0d94c08bd3a44345604a261cd317200848f863a1a7cf

Contents?: true

Size: 650 Bytes

Versions: 1

Compression:

Stored size: 650 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"

    return self if size <= 1

    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

1 entries across 1 versions & 1 rubygems

Version Path
rand_sum-0.2.1 lib/rand_sum.rb