# Inserting a random item from an array

**URL:** <https://www.ruby-forum.com/t/inserting-a-random-item-from-an-array/143410>\
**Category:** Ruby\
**Created:** [July 18, 2008, 7:10am UTC](https://www.ruby-forum.com/t/inserting-a-random-item-from-an-array/143410 "2008-07-18T07:10:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [July 18, 2008, 7:10am UTC](https://www.ruby-forum.com/t/inserting-a-random-item-from-an-array/143410/1 "2008-07-18T07:10:23Z")

</div>

I’m trying to have Ruby generate a random list of 30 links to put at the  
bottom of a web page and write it to a file. I think I’m on the right  
track of how to do it, but I’m not sure…

##############  
citylist = [‘Orlando’,  
‘Tampa’,  
‘Fort Lauderdale’,  
‘Miami’,  
‘St. Petersburg’,  
‘Etc.’  
]

output =  
30.times do [rand(citylist.length)]  
%Q(Website  
#{rand(citylist.length)} | )  
end

File::open(“I:\My Documents\Stuff\More Stuff\websitelinks.txt”, ‘w’)  
do |f|  
f.write output  
end

##############

I may be mistaken about the use of the block here… Someone please tell  
me what I’m doing wrong and how to fix it? The two instances of the  
city inside the block should be the same, but I’m not sure how to do  
that. Doesn’t rand make it a random city each time it’s interpreted or  
since it’s in a block, would it be the same each time the block  
repeats…?

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [July 18, 2008, 10:06am UTC](https://www.ruby-forum.com/t/inserting-a-random-item-from-an-array/143410/2 "2008-07-18T10:06:41Z")

</div>

On Fri, Jul 18, 2008 at 7:05 AM, Zoe P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## ‘Etc.’ do |f| repeats…?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hmm I would just do this  
File::open(…,“w”) do | f |  
f.puts citylist.sort\_by{ rand }[0…30].  
map { | florida\_agglomoration |  
%{a href="http:…#{florida\_agglomoration} etc.etc. }  
}  
end

Your above code created potential doubletons, did not assure the  
consistency between links and the text.

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

AALST (n.) One who changes his name to be further to the front  
D.Adams; The Meaning of LIFF

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [July 18, 2008, 11:29am UTC](https://www.ruby-forum.com/t/inserting-a-random-item-from-an-array/143410/3 "2008-07-18T11:29:55Z")

</div>

Zoe P. wrote:

> output =  
> 30.times do [rand(citylist.length)]

First of all Integer#times returns self, so 30.times will return 30  
which  
means that the value of output will be 30.  
Second of all rand(foo) returns a number between 0 (inclusive) and foo  
(exclusive). [rand(foo)] will simply return an array containing that  
number.  
So the value of [rand(citylist.length)] may be [5] or [4], but it will  
never  
be a city name. Also as you don’t do anything with the value the whole  
thing  
won’t have any effect on the result of the program.

> %Q([W  
> ebsite #{rand(citylist.length)}](http://www.website.com/cities/website#{rand(citylist.length)}.html) | )  
> end

This will create links like “…website1.html”, “website5.html” etc. It  
won’t  
contain city names. If you want a random city name you have to do:  
citylist[rand(citylist.length)] which might evaluate to citylist[5] for  
example which will evaluate to a city name.  
Also as with the previous array you don’t do anything with the string  
you  
created here (the string will be the return value of the block, but the  
times  
method doesn’t care about the block’s return value, so it will be  
discarded).

> The two instances of the  
> city inside the block should be the same, but I’m not sure how to do  
> that.

As I explained, there aren’t any cities in the block, just numbers. And  
those  
are not the same (except if rand returns the same number twice by  
coincidence)  
If you want to use one random city multiple times do:  
city = citylist[rand(citylist.length)]  
And then do stuff with city. If you want to remove that city from the  
list (so  
you won’t pick it again when you take another random city), you can use:  
city = citylist.delete\_at(rand(citylist.length))  
So now city might be “miami” and the citylist will no longer contain  
miami.

> Doesn’t rand make it a random city each time it’s interpreted or  
> since it’s in a block, would it be the same each time the block  
> repeats…?

It makes a random number each time it’s called.

Here’s some code that should work:  
output = Array.new(30) do  
city = citylist.delete\_at(rand(citylist.length))  
%Q([Website  
#{city}](http://www.website.com/cities/website#{city}.html) | )  
end  
File.open(filename, ‘w’) do |f|  
f.write output  
end

(Array.new(x) will execute the block x times and store the result of the  
block  
in the array after each execution)  
Or if you don’t need the temporary array that creates:  
File.open(filename, “w”) do |f|  
30.times do  
city = citylist.delete\_at(rand(citylist.length))  
f.puts %Q([Website  
#{city}](http://www.website.com/cities/website#{city}.html) | )  
end  
end

Of course in both solutions citylist will be modified in the process  
(i.e. the  
30 cities will be removed). If that’s a problem you should use Robert’s  
solution (which has the upside that it doesn’t affect citylist and the  
downside that it sorts the entire list, which might be undesirable if  
the  
list is considerably larger than 30 entries).

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [July 18, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/inserting-a-random-item-from-an-array/143410/4 "2008-07-18T21:02:32Z")

</div>

I’m fairly new at programming with Ruby (please forgive my  
n00bish-ness), but since I’m having to learn the majority of this on my  
own through online documentation and tutorials, it’s a little difficult  
at times. I guess the best learning is done by doing and well… I’m  
trying to do. :-p

As far as the code I had returning numbers instead of strings, I noticed  
that it was doing that when I ran my earlier code in the command line,  
but I wasn’t sure exactly why. Thanks for explaining.

What I’m trying to do is generate a web page for each city in the array  
that I have and have random links at the bottom of each page to the  
other city pages that will be generated by the program.

The city list that I’m using has around 200 entries in it and I’d like  
to have 30 random links to pages in that list at the bottom of each  
page. So, the list is considerably larger than 30 entries and I don’t  
want an entry to be deleted each time its used.
