require 'date' exclude_days = [6,7] total_hours_worked = remaining_hours = 107 max_hours_per_day = 5 dates = (Date.new(2006,2,26)..Date.new(2006,7,26)).to_a date_log = {} puts "Randomly distributing #{total_hours_worked} hours over #{dates.size} days." while remaining_hours > 0 do h = rand(max_hours_per_day+1) if rand(2) == 0 && h > 1 h -= 0.5 else h +- 0.5 end h = remaining_hours if h > remaining_hours unless h == 0 loop do day = rand(dates.size + 1) - 1 unless date_log.has_key?(day) || exclude_days.include?(dates[day].cwday) date_log[day] = h break end end end remaining_hours -= h end 0.upto(dates.size) do |n| if date_log.has_key?(n) puts "#{dates[n]} #{date_log[n]}" end end