- Home /
Generate a random string from a specified length.
Could someone help me make a code snippet (in C#) that can generate a random string (and assign it to a variable, which should be easy) from a specified length from an "int" variable (which will be slightly randomized) for a game item that is supposed to be glitchy. It'd be great if it could be done so it also generates special characters (î, ä, é, ç, ø, ¡, å, †, ®, ∂, ˙, ©, ¬, ˚, ˜, all of which I wrote by holding down option and a random letter, So I know it's basic ASCII)
Any help will be greatly appreciated!
~Juan Callejas
None of those characters are basic ASCII, but Unity doesn't use ASCII anyway, it uses Unicode.
Right. $$anonymous$$istook those. I don't know that much about it, but those extra chars are Unicode, right?
Answer by barbe63 · May 13, 2015 at 02:52 AM
Set a string to be the array of your authorized characters like this:
const string glyphs= "abcdefghijklmnopqrstuvwxyz0123456789"; //add the characters you want
Then after declaring your string variable(here myString), do this:
int charAmount = Random.Range(minCharAmount, maxCharAmount); //set those to the minimum and maximum length of your string
for(int i=0; i<charAmount; i++)
{
myString += glyphs[Random.Range(0, glyphs.Length)];
}
Will this make it flicker or make it noticeable that it's changing? Because I plan on having it flicker random strings constantly
Interesting study about the 3 methods : http://fr.slideshare.net/sindharta/c-string-concatenations-in-unity I don't understand your last question.
I'm guessing by flickering you mean you want the string to randomize regularly during the gameplay to simulate some data "error" or an obscured text. To do that you would have to call this function barbe63 provided at an interval. I would suggest this for that.
Your answer
Follow this Question
Related Questions
How to make a list with unique strings randomly 1 Answer
Distribute terrain in zones 3 Answers
Random role assigning from string array 1 Answer
(c#) display a random string at game over 2 Answers
Multiple Cars not working 1 Answer