Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Randomise character positions in string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike P - 25 Feb 2008 12:28 GMT
Does anybody have a simple method for randomising the character
positions in a string?
Roger Frost - 25 Feb 2008 12:41 GMT
Like a scramble method where "Hello, World!" becomes something like
"leol,H!o rlWd" ?

Signature

Roger Frost
"Logic Is Syntax Independent"

> Does anybody have a simple method for randomising the character
> positions in a string?
>
> *** Sent via Developersdex http://www.developersdex.com ***
Roger Frost - 25 Feb 2008 12:49 GMT
Oh, and at some point will you need to restore the string to it's original
state?

Signature

Roger Frost
"Logic Is Syntax Independent"

> Like a scramble method where "Hello, World!" becomes something like
> "leol,H!o rlWd" ?
Mike P - 25 Feb 2008 12:55 GMT
Roger,

Yes, exactly like that, but I won't need to restore the string to its
original state.

Thanks,

Mike
Marc Gravell - 25 Feb 2008 12:48 GMT
How about (off the top of my head):

       public static string Scramble(string input) {
           if(string.IsNullOrEmpty(input)) return input; // GIGO

           List<char> inputChars = new List<char>(input);
           char[] outputChars = new char[inputChars.Count];
           Random rand = new Random();
           // fill backwards to simplify Rand.Next() logic
           for (int i = inputChars.Count - 1; i >= 0; i--)
           {
               int index = rand.Next(i);
               outputChars[i] = inputChars[index];
               inputChars.RemoveAt(index);
           }
           return new string(outputChars);
       }

       public static void Main()
       {
           string foo = "The quick brown fox jumps over the lazy
dog";
           string bar = Scramble(foo);
       }
Marc Gravell - 25 Feb 2008 12:58 GMT
Sorry, should have been Next(i+1), otherwise it always starts with the
last character.

Actually, I'm amazed it didn't throw an exception, passing Next(0)
[although this is covered in the MSDN documentation, still...]

Marc
Mike P - 25 Feb 2008 14:20 GMT
Many thanks Marc!
Arne Vajhøj - 26 Feb 2008 03:02 GMT
> Does anybody have a simple method for randomising the character
> positions in a string?

using System;

namespace E
{
    public class Program
    {
        private static Random rng = new Random();
        public static void Main(string[] args)
        {
            string s = "Hello world !";
            char[] c = s.ToCharArray();
            byte[] b = new byte[c.Length];
            rng.NextBytes(b);
            Array.Sort<byte, char>(b, c);
            string s2 = new string(c);
            Console.WriteLine(s2);
            Console.ReadKey();
        }
    }
}

It is not the most efficient method, but the code is simple.

Arne

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.