Inserting a random item from an array

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…?

On Fri, Jul 18, 2008 at 7:05 AM, Zoe P.
[email protected] wrote:

‘Etc.’
do |f|
repeats…?

Posted via 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/


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

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)} | )
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}
| )
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}
| )
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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs