1

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!

asked Jan 18, 2021 at 19:44
3
  • 1
    Well that would always produce 5 identical characters. Like AAAAA what have you tried Commented Jan 18, 2021 at 19:53
  • There is a String() ctor that takes a char and returns a string of it repeated n times. Commented Jan 18, 2021 at 19:54
  • Can you give some examples of what your pre-glitched and post-glitched strings would look like? Commented Jan 18, 2021 at 19:55

3 Answers 3

1

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;
answered Jan 18, 2021 at 20:50
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Jan 18, 2021 at 21:46

Comments

1

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);
 }
answered Jan 18, 2021 at 22:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.