Choisissez un personnage aléatoire

j’ai quelques personnages:

chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray(); 

Maintenant, je cherche une méthode pour renvoyer un caractère aléatoire de ceux-ci.

J’ai trouvé un code qui peut être utile:

 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; } 

ce code me renvoie un caractère aléatoire de l’alphabet mais ne me renvoie que des lettres minuscules

Eh bien, vous y êtes presque – vous voulez renvoyer un élément aléatoire d’une chaîne, de sorte que vous générez simplement un nombre aléatoire dans la plage de la longueur de la chaîne:

 public static char GetRandomCharacter(ssortingng text, Random rng) { int index = rng.Next(text.Length); return text[index]; } 

Je vous déconseille par ailleurs d’ utiliser une variable static de type Random sans aucun locking – Random n’est pas thread-safe. Voir mon article sur les nombres aléatoires pour plus de détails (et solutions de contournement).

Cela pourrait fonctionner pour vous:

 public static char GetLetter() { ssortingng chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&"; Random rand = new Random(); int num = rand.Next(0, chars.Length -1); return chars[num]; } 

Vous pouvez l’utiliser comme;

 char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray(); Random r = new Random(); int i = r.Next(chars.Length); Console.WriteLine(chars[i]); 

Voici une DEMO .

J’avais un problème approximatif et je l’ai fait de cette façon:

 public static Ssortingng GetRandomSsortingng() { 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); } 

Au lieu de 26, veuillez utiliser la taille de votre tampon CHARS.

 int num = random.Next(0, chars.Length) 

Puis au lieu de

 let = (char)('a' + num) 

utilisation

 let = chars[num] 
 private static void Main(ssortingng[] args) { Console.WriteLine(GetLetters(6)); Console.ReadLine(); } public static ssortingng GetLetters(int numberOfCharsToGenerate) { var random = new Random(); char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray(); var sb = new SsortingngBuilder(); for (int i = 0; i < numberOfCharsToGenerate; i++) { int num = random.Next(0, chars.Length); sb.Append(chars[num]); } return sb.ToString(); } 

Je souhaite que ce code vous aide:

  ssortingng s = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&"; Random random = new Random(); int num = random.Next(0, s.Length -1); MessageBox.Show(s[num].ToSsortingng()); 

Obtenir le caractère du numéro ASCII:

 private ssortingng GenerateRandomSsortingng() { Random rnd = new Random(); ssortingng txtRand = ssortingng.Empty; for (int i = 0; i <8; i++) txtRand += ((char)rnd.Next(97, 122)).ToString(); return txtRand; } 

Vous pouvez essayer ceci:

  public static ssortingng GetPassword() { ssortingng Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; Random rnd = new Random(); int index = rnd.Next(0,51); ssortingng char1 = Characters[index].ToSsortingng(); return char1; } 

Maintenant, vous pouvez jouer avec ce bloc de code selon vos souhaits. À votre santé!

Je ne suis pas sûr de son efficacité, car je suis très novice en matière de codage. Cependant, pourquoi ne pas utiliser simplement le nombre aléatoire que vous créez déjà? Est-ce que cela ne “randomise” pas un caractère en majuscule aussi?

  int num = random.Next(0,26); char let = (num > 13) ? Char.ToUpper((char)('a' + num)) : (char)('a' + num); 

En outre, si vous souhaitez prendre une seule lettre de votre caractère [], serait-il plus facile d’utiliser une simple chaîne?

  ssortingng charRepo = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&"; Random rando = new Random(); int ranNum = rando.Next(0, charRepo.Length); char ranChar = charRepo[ranNum];