c# - how to make random loop for array -


i want make code simple: take word on english , make random array character of word. intense, word:"qweasdzxc" should represent that: "adwseqzcx" (random) . code is:

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks;  namespace randomloop {   class program  {     static void main(string[] args)     {         string s = "qweasdzxc";         string[] q = newarray(s);         (int = 1; < s.length - 1; i++)         {             console.write(q[i]);         }     }      public static char[] getarrayofstring(string s)     {         char[] c = s.tochararray();         return c;      }      public static random rnd = new random();      public static string[] charandnumber(char c, int lengthoftheword)//0-char 1-integer     {         string[] array = new string[2];         array[0] = c.tostring();         array[1] = (rnd.next(lengthoftheword)).tostring();         return array;      }      public static string[] newarray(string s)     {         string[] c = new string[s.length];         int j = 1;         string[] q = charandnumber(s[j], s.length - 1);          (int = 1; < c.length - 1; i++)         {             c[i] = "";          }         int e = 1;          while (e.tostring() != q[1])         {              c[e] = q[0];             j++;//for character s[j] see             e++;//for loop         }           return c;     } } 

}

it seems want shuffle characters , generate new string.you can use method uses fisher-yates shuffle algorithm (from this answer, modified little bit situation):

public static void shuffle<t>(this t[] source)   {       random rng = new random();       int n = source.length;      while (n > 1) {           n--;           int k = rng.next(n + 1);           t value = source[k];           source[k] = source[n];           source[n] = value;       }   } 

then use it:

string s = "qweasdzxc"; char[] chars = s.tochararray(); chars.shuffle(); string random = new string(chars); 

also remember extension method need put public , static class.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -