Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

array#rand syntax question

the following code is essential array#sample

def random_select(array, n)
  new_array = []
  n.times do 
    new_array << array[rand(array.length)]
  end 

  return new_array 
end 

My question is for

array[rand(array.length)]

why is it not array.rand(array.length)? I have tried to look for an explanation on the syntax but couldn't find one. Any help is much appreciated! thank you!

3 Answers

Hey Lawrence,

No worries! I understand what you're asking now.

The ruby rand command is going to pick a random number using the length of the array that is passed in through the methods parameters.

Lets say I make a random array of numbers:

array_example = [1, 2, 3, 4, 5, 6, 7, 8]

And I use the array I just made and plugged it into your code from above:

random_select(array_example, 2)

Then the following should happen and other people should please correct me if I'm wrong. The rand command will take the length of my array_example and pick a random number from it.

From this:

def random_select([1, 2, 3, 4, 5, 6, 7, 8], 2)
  new_array = []
  2.times do 
    new_array << [1, 2, 3, 4, 5, 6, 7, 8][rand([1, 2, 3, 4, 5, 6, 7, 8].length)]
  end 

  return new_array 
end

To this:

def random_select([1, 2, 3, 4, 5, 6, 7, 8], 2)
  new_array = []
  2.times do 
    new_array << [1, 2, 3, 4, 5, 6, 7, 8][rand(8)]
  end 

  return new_array 
end

So now it'll just pick a random number from 0 to 7 and pick the number in that index. If rand(8) come out as 7, the number in the 7th index would be picked which is the number 8.

I hope that makes sense to you. Let me know if any part of this needs more clarifying.

Thanks!

Hey Lawrence,

Check out this link for a good explanation: https://www.ruby-forum.com/topic/4403714

It looks like there is no array.rand but there is an array.sample method that will, "Choose a random element or n random elements from the array." Check out: http://www.ruby-doc.org/core-2.0/Array.html#method-i-sample

But the code above looks pretty snazzy.

Although, you could build your own rand method for the Array class if you'd so choose to do. :)

I hope that helps.

Hey Neil,

thanks for the comment! I apologize I should have clarified what I was asking. My question is what does the brackets in the code below do?

array[rand(array.length)]

I have never seen that syntax and could not find an answer so wanted to ask the treehouse community. thanks!