From: Alex Young Date: 2007-11-27T18:36:41+09:00 Subject: Re: Most elegant way to do this? Robert Klemme wrote: > 2007/11/27, Phrogz : >> On Nov 26, 5:24 pm, Alex Young wrote: >>> rbysam...@gmail.com wrote: >>>> Are there any more elegant, concise, pithy, and more Rubyish ways of >>>> doing this? >>>> def roll(number_of_dice) >>>> sum = 0 >>>> number_of_dice.times do >>>> sum += rand(5).next >>>> end >>>> sum >>>> end >>> The Incredible Inevitable Inject: >>> >>> def roll(number_of_dice) >>> (0...number_of_dice).inject(0){|m,r| rand(5)+m} >>> end >> You missed the +1 needed to take rand(5) to 1..6 instead of 0..5. > > And everybody apparently missed 6 because rand(5) will yield *5* > values ranging in 0..4. :-)) http://hometown.aol.com/dicetalk/polymor2.htm Nobody said dice have to be cubic... -- Alex