# \# arguments to rand

**URL:** <https://www.ruby-forum.com/t/arguments-to-rand/130227>\
**Category:** Ruby\
**Created:** [February 26, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/arguments-to-rand/130227 "2008-02-26T15:18:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 26, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/arguments-to-rand/130227/1 "2008-02-26T15:18:13Z")

</div>

hi -

can someone help with suggesting what is wrong below? i am trying to  
add a simple method to array#random

but get the wrong number of args error…  
have tried lots of variations!

> > class Array  
> > def random  
> > n = rand(self.size.to\_i)  
> > return self[n]  
> > end  
> > end  
> > =\> nil
> 
> > [1, 2, 3].random  
> > ArgumentError: wrong number of arguments (1 for 0)  
> > from (irb):14:in `rand' from (irb):14:in`random’  
> > from (irb):18
> 
> > rand(3)  
> > =\> 1
> 
> > rand(3)  
> > =\> 1
> 
> > rand(3)  
> > =\> 2

tx

/dc

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 26, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/arguments-to-rand/130227/2 "2008-02-26T15:22:59Z")

</div>

Alle Tuesday 26 February 2008, d c ha scritto:

> > > ```
> > > n = rand(self.size.to_i)
> > > 
> > > ```
> 
> from (irb):14:in `random’
> 
> > > rand(3)
> 
> =\> 2
> 
> tx
> 
> /dc

It works for me. By the way, you don’t need the to\_i in the call to  
rand:  
Array#size is already and integer.

Stefano

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 26, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/arguments-to-rand/130227/3 "2008-02-26T15:43:40Z")

</div>

On Tue, Feb 26, 2008 at 3:14 PM, d c [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ```
> > > n = rand(self.size.to_i)
> > > 
> > > ```
> 
> =\> 1

Works for me.

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 26, 2008, 3:53pm UTC](https://www.ruby-forum.com/t/arguments-to-rand/130227/4 "2008-02-26T15:53:45Z")

</div>

On 26/02/2008, d c [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ```
> > > n = rand(self.size.to_i)
> > > 
> > > ```

You are probably calling Array#rand as defined by Rails. If so, it  
does what your Array#random is supposed to do and you can use it  
directly. Otherwise you can call Kernel.rand instead of just rand.

Peter

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 26, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/arguments-to-rand/130227/5 "2008-02-26T16:12:28Z")

</div>

d c wrote:

> can someone help with suggesting what is wrong below?

You’re monkey patching a built-in class. 😉

Best regards,

Jari W.

> i am trying to

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 26, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/arguments-to-rand/130227/6 "2008-02-26T16:44:53Z")

</div>

David C. wrote:

> > > class Array  
> > > def random  
> > > n = rand(self.size.to\_i)  
> > > return self[n]  
> > > end  
> > > end  
> > > =\> nil
> > 
> > > [1, 2, 3].random  
> > > ArgumentError: wrong number of arguments (1 for 0)  
> > > from (irb):14:in `rand’

Did you by any chance alias random to rand in Array? If so then that  
would be the reason.  
Anyway, maybe you can use this one, it’s what I use for Array#random:

class Array

# Get a random element of this array

# With an argument it gets you n elements without any index used more

than once  
def random(n=nil)  
unless n then  
at(rand(length))  
else  
raise ArgumentError unless Integer === n and n.between?(0,length)  
ary = dup  
l = length  
n.times { |i|  
r = rand(l-i)+i  
ary[r], ary[i] = ary[i], ary[r]  
}  
ary.first(n)  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 26, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/arguments-to-rand/130227/7 "2008-02-26T17:14:38Z")

</div>

On Feb 26, 8:52 am, Calamitas [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > class Array  
> > > > def random  
> > > > n = rand(self.size.to\_i)
> 
> You are probably calling Array#rand as defined by Rails. If so, it  
> does what your Array#random is supposed to do and you can use it  
> directly. Otherwise you can call Kernel.rand instead of just rand.
> 
> Peter

This is what I was going to bring up. I’d also like to add that it was  
a wonderful decision on Norbert Crombach’s part to name the method  
Array#rand rather than Array#random so that confusion and/or annoyance  
would set in were one to attempt to add another method to Array that  
attempts to use random numbers. Oh, also the Rails core team in  
general for not bringing this up and accepting the patch without a  
comment.
