Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Most elegant way to do this?

Reply
Thread Tools

Most elegant way to do this?

 
 
rbysamppi@gmail.com
Guest
Posts: n/a
 
      11-27-2007
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

Thanks in advance!
 
Reply With Quote
 
 
 
 
Alex Young
Guest
Posts: n/a
 
      11-27-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
>
> Thanks in advance!
>


The Incredible Inevitable Inject:

def roll(number_of_dice)
(0...number_of_dice).inject(0){|m,r| rand(5)+m}
end

--
Alex

 
Reply With Quote
 
 
 
 
JC
Guest
Posts: n/a
 
      11-27-2007
(E-Mail Removed) 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
>
> Thanks in advance!


I don't think there's really anything you can do to it, but I would
suggest that you add a parameter to set the sides of the dice and also
add one to the random output (if it's the random number gen I'm thinking
of, it'll give you 0-5 which means your dice have a blank side )

-JC
 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      11-27-2007
On Nov 26, 5:24 pm, Alex Young <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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 I personally like 1..num_dice instead of 0...num_dice. And,
finally, I sum things so often I usually have this lying around:

module Enumerable
def sum
if block_given?
inject(0){ |sum,obj| sum + yield(obj) }
else
inject(0){ |sum,obj| sum+obj }
end
end
end

which makes the solution simply:
def roll(number_of_dice)
(1..number_of_dice).sum{ rand(5)+1 }
end


In the vein of DRY code and unix tools, I strongly encourage everyone
to be on constant vigil looking for bits of code that can be
abstracted out to little atomic re-usable bits. After a while, coding
is less like carving entire models from styrofoam, and more like
snapping little Lego blocks together.
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-27-2007
2007/11/27, Phrogz <(E-Mail Removed)>:
> On Nov 26, 5:24 pm, Alex Young <(E-Mail Removed)> wrote:
> > (E-Mail Removed) 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. )

> And I personally like 1..num_dice instead of 0...num_dice. And,
> finally, I sum things so often I usually have this lying around:
>
> module Enumerable
> def sum
> if block_given?
> inject(0){ |sum,obj| sum + yield(obj) }
> else
> inject(0){ |sum,obj| sum+obj }
> end
> end
> end
>
> which makes the solution simply:
> def roll(number_of_dice)
> (1..number_of_dice).sum{ rand(5)+1 }
> end


There is another one, that - at least theoretically - saves some
addition efforts:

require 'enumerator'

def roll(number)
raise ArgumentError, "Negative!" if number < 0
number.to_enum(:times).inject(number) {|s,| s + rand(6)}
end

I also threw in to_enum just for the fun of it.

> In the vein of DRY code and unix tools, I strongly encourage everyone
> to be on constant vigil looking for bits of code that can be
> abstracted out to little atomic re-usable bits. After a while, coding
> is less like carving entire models from styrofoam, and more like
> snapping little Lego blocks together.


Absolutely!

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-27-2007
2007/11/27, Robert Klemme <(E-Mail Removed)>:
> There is another one, that - at least theoretically - saves some
> addition efforts:


Of course I wanted to say that it /practically/ saves addition efforts
and /theoretically/ it will also save time.

Cheers

robert

 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      11-27-2007
Phrogz wrote:
> On Nov 26, 5:24 pm, Alex Young <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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.

Oops

--
Alex

 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      11-27-2007
Robert Klemme wrote:
> 2007/11/27, Phrogz <(E-Mail Removed)>:
>> On Nov 26, 5:24 pm, Alex Young <(E-Mail Removed)> wrote:
>>> (E-Mail Removed) 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

 
Reply With Quote
 
Heesob Park
Guest
Posts: n/a
 
      11-27-2007
Hi,
unknown 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
>
> Thanks in advance!


If you want the dice number, you should use rand(6).

How about this:

def roll(n)
eval('+rand(6)+1'*n)
end

Regards,

Park Heesob
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      11-27-2007
On Nov 26, 6:17 pm, (E-Mail Removed) 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
>
> Thanks in advance!


def sum *x
x.inject{|a,b| a+b}
end
def roll n
sum( *(1..n).map{rand(6)+1} )
end
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Most elegant way: array of files from array of directories Terry Michaels Ruby 4 10-16-2010 10:39 PM
What's the most elegant way of doing this? Alex Buell C Programming 3 02-04-2009 10:07 AM
Most elegant way to 'reroute' Text widget events? Kenneth McDonald Ruby 2 09-02-2008 03:48 AM
Most elegant way to clear all the text fields on asp.net page? Duk Lee ASP .Net 2 07-23-2007 08:25 PM
Most elegant way to generate 3-char sequence Rob Cowie Python 28 06-13-2006 01:40 AM



Advertisments