- Home /
Random char A to Z
I'm sure its simple, but i cant figure it out. i'm trying to make å random char/letter generator in c#. Is there a easy way to do this?
thanks=)
Answer by robertbu · Sep 30, 2014 at 04:53 PM
The most flexible and easiest is to create a string and use Random.Range(). The string can contain any letters, or the same letters in multiple cases:
string st = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
And then you would generate the letter by:
char c = st[Random.Range(st.Length)];
It can also be done by generating ASCII values. For most character sets, you can do:
char c = (char)('A' + Random.Range (0,26));
Debug.Log (c);
nice, I forgot you could use a string as a list automagically :)
Answer by Scribe · Sep 30, 2014 at 04:53 PM
There might be an easier way of doing this using some ToString format method but I am not too familiar with them so here is how I would do it with an array:
Setup your array.
string[] Alphabet = new string[26] {"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"};
Choose a random index.
Debug.Log(Alphabet[Random.Range(0, Alphabet.Length)]);
Hope that helps!
Scribe
Answer by ReyGrau · Aug 29, 2021 at 08:22 AM
public char GerRandomCharA2Z()
{
return (char)Random.Range('a', 'z');
}