Reputation:
i have some chars:
chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
now i'm looking for a method to return a random char from these.
I found a code which maybe can be usefull:
static Random random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter
// ... Between 'a' and 'z' inclusize.
int num = random.Next(0, 26); // Zero to 25
char let = (char)('a' + num);
return let;
}
this code returns me a random char form the alphabet but only returns me lower case letters
Upvotes: 28
Views: 89256
Reputation: 4192
This might work for you:
public static char GetLetter()
{
string chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
Random rand = new Random();
int num = rand.Next(0, chars.Length);
return chars[num];
}
Upvotes: 15
Reputation: 1405
Your Code is good, you just need to change 'a' to 'A'
static Random random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter
// ... Between 'a' and 'z' inclusize.
int num = random.Next(0, 26); // Zero to 25
char let = (char)('A' + num);
return let;
}
This code same as mentioned in the question , just change char let = (char)('A' + num);
, it returns upper case letters.
Thanks!!!
Upvotes: 4
Reputation: 55
I'm not sure how efficient it is as I'm very new to coding, however, why not just utilize the random number your already creating? Wouldn't this "randomize" an uppercase char as well?
int num = random.Next(0,26);
char let = (num > 13) ? Char.ToUpper((char)('a' + num)) : (char)('a' + num);
Also, if you're looking to take a single letter from your char[], would it be easier to just use a string?
string charRepo = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
Random rando = new Random();
int ranNum = rando.Next(0, charRepo.Length);
char ranChar = charRepo[ranNum];
Upvotes: 3
Reputation:
You can try this :
public static string GetPassword()
{
string Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random rnd = new Random();
int index = rnd.Next(0,51);
string char1 = Characters[index].ToString();
return char1;
}
Now you can play with this code block as per your wish. Cheers!
Upvotes: 0
Reputation: 2228
Instead of 26 please use size of your CHARS buffer.
int num = random.Next(0, chars.Length)
Then instead of
let = (char)('a' + num)
use
let = chars[num]
Upvotes: 3
Reputation: 9
Getting Character from ASCII number:
private string GenerateRandomString()
{
Random rnd = new Random();
string txtRand = string.Empty;
for (int i = 0; i <8; i++) txtRand += ((char)rnd.Next(97, 122)).ToString();
return txtRand;
}
Upvotes: 1
Reputation: 1604
I had approximate issue and I did it by this way:
public static String GetRandomString()
{
var allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
var length = 15;
var chars = new char[length];
var rd = new Random();
for (var i = 0; i < length; i++)
{
chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
}
return new String(chars);
}
Upvotes: 3
Reputation: 3512
I wish This code helps you :
string s = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
Random random = new Random();
int num = random.Next(0, s.Length -1);
MessageBox.Show(s[num].ToString());
Upvotes: 1
Reputation: 98868
You can use it like;
char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
Random r = new Random();
int i = r.Next(chars.Length);
Console.WriteLine(chars[i]);
Here is a DEMO
.
Upvotes: 5
Reputation: 6963
private static void Main(string[] args)
{
Console.WriteLine(GetLetters(6));
Console.ReadLine();
}
public static string GetLetters(int numberOfCharsToGenerate)
{
var random = new Random();
char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
var sb = new StringBuilder();
for (int i = 0; i < numberOfCharsToGenerate; i++)
{
int num = random.Next(0, chars.Length);
sb.Append(chars[num]);
}
return sb.ToString();
}
Upvotes: 2
Reputation: 1504172
Well you're nearly there - you want to return a random element from a string, so you just generate a random number in the range of the length of the string:
public static char GetRandomCharacter(string text, Random rng)
{
int index = rng.Next(text.Length);
return text[index];
}
I'd advise against using a static
variable of type Random
without any locking, by the way - Random
isn't thread-safe. See my article on random numbers for more details (and workarounds).
Upvotes: 45