Hack Forums
[C#] Adding random chars between characters - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Adding random chars between characters (/showthread.php?tid=6092718)



Adding random chars between characters - draxyee - Jul 5, 2020

Hi guys. Trying to generate strings with random "_" between them. However, I'm getting a lot of repeated strings on 100 generations, how can this be? The length of the original string that is being modified is 16 chars. That should allow for tens of thousands of variations?


PHP Code

<?php 
          
var rnd = new ThreadLocal<Random>(() => new Random(Guid.NewGuid().GetHashCode()));

 
          for (int i = 0; i < UserSuggest.Length; i += 2)
 
               {
 
                   int Random = rnd.Value.Next(0, 2);

 
                   switch (Random)
 
                   {
 
                       case 0:
 
                           UserSuggest = UserSuggest.Insert(i, "-");
 
                           break;
 
                   }
 
               } 



RE: Adding random chars between characters - iolaka - Jul 7, 2020

Code
void Main() {
    List <string> unique = new List <string>();
    string UserSuggest = "abcdefghijklmnop";
    Random rnd = new Random();
    while (unique.Count < 1000) {
        string s = UserSuggest;
        for (int i = 1; i < s.Length; i += 2) {
            switch (rnd.Next(0, 2)) {
            case 0:
                s = s.Insert(i, "-");
                break;
            }
        }
        if (!unique.Contains(s)) {
            unique.Add(s);
            Console.WriteLine(s);
        }
    }
}