Jumble character order in a string


#1

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?


#2

Bill K. gave a nicely concise and elegant solution, so I won’t bother
giving mine. But I wanted to ask what this is for? :slight_smile: A game? :slight_smile:

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?


#3

From: “jotto” removed_email_address@domain.invalid

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