# Jumble character order in a string

**URL:** <https://www.ruby-forum.com/t/jumble-character-order-in-a-string/54438>\
**Category:** Ruby\
**Created:** [March 5, 2006, 2:14am UTC](https://www.ruby-forum.com/t/jumble-character-order-in-a-string/54438 "2006-03-05T02:14:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [March 5, 2006, 2:14am UTC](https://www.ruby-forum.com/t/jumble-character-order-in-a-string/54438/1 "2006-03-05T02:14:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [March 7, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/jumble-character-order-in-a-string/54438/2 "2006-03-07T19:10:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [March 5, 2006, 2:23am UTC](https://www.ruby-forum.com/t/jumble-character-order-in-a-string/54438/3 "2006-03-05T02:23:28Z")

</div>

From: “jotto” [removed\_email\_address@domain.invalid](mailto: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
