Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to initialize char array with random characters in C#

3 Answers

0 votes
using System;
 
public class Program
{
    public static void Main()
    {
        char[] array = new char[10];
 
        string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        Random rnd = new Random();
 
        for (int index = 0; index < 10; index++) {
            array[index] = charset[rnd.Next(charset.Length)];
        }
 
        foreach (var ch in array) {
            Console.Write(ch + " ");
        }
    }
}
 
 
 
 
/*
run:
 
v R s I e m u J T r 
 
*/

 



answered Nov 12, 2021 by avibootz
edited May 17, 2024 by avibootz
0 votes
using System;
  
public class Program
{
    public static void initialize_char_array_with_random_characters(char[] array) {
        string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
         
        Random rnd = new Random();
 
        for (int index = 0; index < array.Length; index++) {
            array[index] = charset[rnd.Next(charset.Length)];
        }
    }
     
    public static void Main()
    {
        char[] array = new char[10];
  
        initialize_char_array_with_random_characters(array);
         
        foreach (var ch in array) {
            Console.Write(ch + " ");
        }
    }
}
 
  
  
  
/*
run:
  
z u d M W l d a c o 
  
*/

 

 



answered May 17, 2024 by avibootz
edited May 17, 2024 by avibootz
0 votes
using System;
 
public class Program
{
    public static void initialize_char_array_with_random_characters(char[] array) {
       char[] charset = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
        
        Random rnd = new Random();

        for (int index = 0; index < array.Length; index++) {
            array[index] = charset[rnd.Next(charset.Length)];
        }
    }
    
    public static void Main()
    {
        char[] array = new char[10];
 
        initialize_char_array_with_random_characters(array);
        
        Console.WriteLine(new string(array));
    }
}


 
 
/*
run:
 
IzHB:2@Rz^
 
*/

 



answered May 17, 2024 by avibootz

Related questions

1 answer 63 views
1 answer 77 views
1 answer 85 views
1 answer 92 views
1 answer 79 views
2 answers 94 views
...