Forum: Ruby Jumble character order in a string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6baa3299d90dc0d50940e8d14e828964?d=identicon&s=25 jotto (Guest)
on 2006-03-05 02:14
(Received via mailing list)
I'm trying to figure out a way to randomize the letters in a word,
essentially jumbling the spelling.

jumbling the word jumble would give: lbujme or bujlme etc...

right now i'm considering:

rand(string.length) to get a random index of a letter in the string,
then making a new string one character at a time. but this obviously
doesn't take into account using the letters only once.

any ideas?
4feed660d3728526797edeb4f0467384?d=identicon&s=25 Bill Kelly (Guest)
on 2006-03-05 02:23
(Received via mailing list)
From: "jotto" <jonathan.otto@gmail.com>
> doesn't take into account using the letters only once.
>
> any ideas?

Hi, here's one way:

irb(main):034:0> "abcdefg".split(//).sort_by{rand}.join
=> "gafbedc"


Regards,

Bill
A402df36168b81b31c17adcbb5ae8cf4?d=identicon&s=25 Pistos Christou (pistos)
on 2006-03-07 19:10
Bill Kelly gave a nicely concise and elegant solution, so I won't bother
giving mine.  But I wanted to ask what this is for?  :)  A game?  :)

Pistos

jotto wrote:
> I'm trying to figure out a way to randomize the letters in a word,
> essentially jumbling the spelling.
>
> jumbling the word jumble would give: lbujme or bujlme etc...
>
> right now i'm considering:
>
> rand(string.length) to get a random index of a letter in the string,
> then making a new string one character at a time. but this obviously
> doesn't take into account using the letters only once.
>
> any ideas?
This topic is locked and can not be replied to.