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

···

On 11/02/2007, at 6:25 PM, woodyee wrote:

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!

Sharon Phillips 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

···

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

this is a Ruby Quiz Spoiler!!!
Robert

···

On 2/11/07, Stefan Rusterholz <apeiros@gmx.net> wrote:

Sharon Phillips 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

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

This is not really what you want to do but I cannot post more for now,

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important
things.
-Anonymous

Hi --

···

On Sun, 11 Feb 2007, Stefan Rusterholz wrote:

Sharon Phillips 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
}

You can replace the last three lines with:

   puts example

:slight_smile:

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Robert Dober wrote:

···

On 2/11/07, Stefan Rusterholz <apeiros@gmx.net> 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 Quiz 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

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

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 2/11/07, Robert Dober <robert.dober@gmail.com> wrote:

On 2/11/07, Stefan Rusterholz <apeiros@gmx.net> wrote:
>
> Sharon Phillips 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
>
> --
> 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 Quiz Spoiler!!!
Robert

--

We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important
things.
-Anonymous

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important
things.
-Anonymous

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, Stefan Rusterholz <apeiros@gmx.net> wrote:

Robert Dober wrote:
> On 2/11/07, Stefan Rusterholz <apeiros@gmx.net> 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 Quiz 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

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

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important
things.
-Anonymous

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 Gray II

···

On Feb 11, 2007, at 6:59 AM, Robert Dober 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.