Jumble character order in a string

J

jotto

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

Bill Kelly

From: "jotto said:
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?

Hi, here's one way:

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


Regards,

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top