Randomly Sorting Strings

Hi! I’m trying to write a program that will print out a different
string each time it’s run from a list of strings.

For example:

example = [‘one’, ‘two’, ‘three’]

sort them somehow

‘two’ prints out as the output, for example

run it again and a different string prints out

Make sense? I’ve tried srand & rand but can’t do this with strings. Do
I need to forget the arrays and try something else? I’ve looked at the
Pickaxe book and Ruby Cookbook but can’t find anything. Thanks!

try

example= %w[one two three cat dog cow table]
puts example[rand(example.size)]

Cheers,
Dave

Sharon P. wrote:

try

example= %w[one two three cat dog cow table]
puts example[rand(example.size)]

Cheers,
Dave

Another example would be:

example= %w[one two three cat dog cow table].sort_by { rand }
example.each { |string|
puts string
}

that will print all in random order

my regards

On 2/11/07, Stefan R. [email protected] wrote:

Another example would be:

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

This is not really what you want to do but I cannot post more for now,
this is a Ruby Q. Spoiler!!!
Robert

Hi –

On Sun, 11 Feb 2007, Stefan R. wrote:

example= %w[one two three cat dog cow table].sort_by { rand }
example.each { |string|
puts string
}

You can replace the last three lines with:

puts example

:slight_smile:

David

On 2/11/07, Stefan R. [email protected] wrote:

I apologize. I didn’t read the current quiz.
Does that mean I have to check back with the current quiz when somebody
asks for help in here?

my regards


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

No I do not think so, no need to apologize either :wink:

Well James is the “boss” of the quiz, but the ML and the Quiz are two
distinct matters.
I personally chose to try to avoid to post any material too much related
to
the Quiz and furthermore I point it out to others who might not be
interested in the Quiz.

Quizzes can be spoiled by posts of course, I redrew a quiz once because
the
issue was discussed three days before its due date, actually I think it
was
a good thing because after the discussion I did not think my Quiz was
very
brilliant any more:). James was just great he had another quiz to come
up
with immediately.

AFAIAC your reply has been very thoughtful, do not worry!

Cheers
Robert

P.S. Quiz Advocacy, checking the quiz is a good idea anyway :)))

R.

On 2/11/07, Robert D. [email protected] wrote:

that will print all in random order

my regards


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

This is not really what you want to do but I cannot post more for now,
this is a Ruby Q. Spoiler!!!
Robert

Ok time to clarify.
#sort_by{ rand } is fine, it is my head that is broken, so it is time
for
me to apologize
#sort{ rand } of course is not, but I’ll get new reading glasses soon.

Robert

On Feb 11, 2007, at 6:59 AM, Robert D. wrote:

I personally chose to try to avoid to post any material too much
related to
the Quiz and furthermore I point it out to others who might not be
interested in the Quiz.

Overlap happens. No big deal. It just means I chose well for the
quiz topic and it will be helpful in the future. :wink:

James Edward G. II

Robert D. wrote:

On 2/11/07, Stefan R. [email protected] wrote:

Another example would be:

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

This is not really what you want to do but I cannot post more for now,
this is a Ruby Q. Spoiler!!!
Robert

I apologize. I didn’t read the current quiz.
Does that mean I have to check back with the current quiz when somebody
asks for help in here?

my regards