From: Vadivelan Vatti Date: 2010-09-07T16:01:03+09:00 Subject: Re: Dice Roller David A. Black wrote: > Hi -- > > On Tue, 7 Sep 2010, Phlip wrote: > >>> get a zero? >> while x <= roll.to_i >> puts "Roll " + x.to_s + ": " + rand(max=6).to_s >> end >> >> Take out the gets.chomp: >> >> roll = '12' # gets.chomp >> >> that lets you work on the loop without entering over and over again. > > That changes the behavior of the program pretty significantly, though. > >> then either fix your while loop by incrementing: x += 1 >> >> or use an each loop (which is truly the Ruby way): >> >> (0...roll.to_i).each do |x| >> puts 'etc' >> end > > I'd be inclined to use #times: > > print "How many dice do you want to roll? " > num_rolls = gets.to_i > num_rolls.times do |n| > puts "Roll #{n+1}: #{rand(6) + 1}" > end > > > David > > -- > David A. Black, Senior Developer, Cyrus Innovation Inc. > > The Ruby training with Black/Brown/McAnally > Compleat Philadelphia, PA, October 1-2, 2010 > Rubyist http://www.compleatrubyist.com Hi, I have made a small change, but both will returns the same result.. By adding the min value to the rand(max-min), we can get random number bw the range(min,max). print "How many dice do you want to roll? " num_rolls = gets.to_i num_rolls.times do |n| puts "Roll #{n+1}: #{1 + rand(6-1)}" end Thanks, Vaddi -- Posted via http://www.ruby-forum.com/.