How do I repeat generating a random char? I want to make a glitched text effect so I set:
textbox.text = st[Random.Range(0, st.Length)];
in my update method. I do however only get one character out of this line - how would I repeat this process in a string with the length of 5? What I am trying to achieve is to randomly generate 5 characters over and over again. There must be a better way than this:
randomChar + randomChar + randomChar + randomChar + randomChar
Thank you in advance!
- 
 1Well that would always produce 5 identical characters. Like AAAAA what have you triedBugFinder– BugFinder2021年01月18日 19:53:39 +00:00Commented Jan 18, 2021 at 19:53
- 
 There is a String() ctor that takes a char and returns a string of it repeated n times.Alex K.– Alex K.2021年01月18日 19:54:13 +00:00Commented Jan 18, 2021 at 19:54
- 
 Can you give some examples of what your pre-glitched and post-glitched strings would look like?Flydog57– Flydog572021年01月18日 19:55:42 +00:00Commented Jan 18, 2021 at 19:55
3 Answers 3
You can use Linq to generate any number of random characters from an enumerable:
int howManyChars = 5;
var newString = String.Join("", Enumerable.Range(0, howManyChars).Select(k => st[Random.Range(0, st.Length)]));
textbox.text = newString;
Comments
If you want completely glitchy strings, go the way of Mojibake. Take a string, convert it to one encoding and then decode it differently. Most programmers end up familiar with this kind of glitch eventually. For example this code:
const string startWith = "Now is the time for all good men to come to the aid of the party";
var asBytes = Encoding.UTF8.GetBytes(startWith);
var glitched = Encoding.Unicode.GetString(asBytes);
Debug.WriteLine(glitched);
consistently results in:
潎7獩琠敨琠浩潦污潧摯洠湥琠潣敭琠桴楡景琠敨瀠牡祴
But, if you just want some text with some random glitches, how about something like this. It uses a set of characters that should be glitched in, a count of how many glitches there should be in a string (based on the string length) and then randomly inserts glitches:
private static Random _rand = new Random();
private const string GlitchChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%&";
public static string GlitchAString(string s)
{
 var glitchCount = s.Length / 5;
 var buffer = s.ToCharArray();
 for (var i = 0; i < glitchCount; ++i)
 {
 var position = _rand.Next(s.Length);
 buffer[position] = GlitchChars[_rand.Next(GlitchChars.Length)];
 }
 var result = new string(buffer);
 Debug.WriteLine(result);
 return result;
}
The first two times I ran this (with that same Now is the time... string), I got:
Original String:
 Now is the time for all good men to come to the aid of the party
First Two Results:
 LowmiW HhZ7time for all good mea to comX to ths aid oV the p1ray
 Now is fhO P!me forjall gKod men to @ome to the a@d of F5e Nawty
The algorithm is, to some extent, tunable. You can have more or fewer glitches. You can change the set of glitch chars - whatever you'd like.
Comments
Could use something similar:
 public static readonly char[] CHARS = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
 static void Main(string[] args)
 {
 
 static string GenerateGlitchedString(uint wordCount,uint wordslength)
 {
 string glitchedString = "";
 for (int i = 0; i < wordCount; i++) 
 {
 for (int j = 0; j < wordslength; j++) 
 {
 Random rnd = new Random();
 glitchedString += CHARS[rnd.Next(CHARS.Length)];
 }
 glitchedString += " "; //Add spaces
 }
 return glitchedString;
 }
 string output = GenerateGlitchedString(5, 5);
 Console.WriteLine(output);
 }
Comments
Explore related questions
See similar questions with these tags.