0

Is there a simple way in Unity C# to shuffle the alphabet letters in a string? This is to create a shuffled version of a string.

leppie
  • 115,091
  • 17
  • 196
  • 297
jadkins4
  • 903
  • 7
  • 18
  • 34
  • Show us your attempt or cheat and just search for one of the 1000 answers for this already. – leppie Dec 19 '14 at 20:53
  • I've been searching for a couple days and tried about 50 examples, including several versions of my own. Right now, I'm back to an empty function. Thanks anyway. – jadkins4 Dec 19 '14 at 21:13
  • 1
    That is good! Learning through trial and error is really the best way to learn. But you need to try and find out what you did wrong. That lesson will stick to you for life. Show us your best attempt. – leppie Dec 19 '14 at 21:19

2 Answers2

-1

Google Fisher-Yates and you'll find this.

To shuffle an array a of n elements (indices 0..n-1):   
    for i from n − 1 downto 1 do
       j ← random integer with 0 ≤ j ≤ i
       exchange a[j] and a[i]

That should be easy enough to translate into c#

RegularExpression
  • 3,531
  • 2
  • 25
  • 36
  • Or just look at the C# implementation provided by duplicate questions on this site. – Scott Chamberlain Dec 19 '14 at 21:01
  • Except is't not possible to modify a string. So to use this you have to create an array of characters from the string, shuffle the array, and then convert back to string. To make this a complete answer, you should include that information. – Jim Mischel Dec 19 '14 at 21:46
-1

order your string by something unpredictable

var oldString = "shuffle";
var shuffled = new string(oldString.OrderBy(x => Guid.NewGuid()).ToArray());

//shuffled = "feflhus"
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112