How to initialize a char array with random characters in C#

6 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
0 votes
using System;

class Program
{
    // Generates an array of random lowercase letters
    static char[] GenerateRandomLowercaseArray(int length) {
        char[] arr = new char[length];
        Random rand = new Random();

        for (int i = 0; i < length; i++) {
            // 'a' is ASCII 97; add 0–25 to get a–z
            arr[i] = (char)('a' + rand.Next(26));
        }

        return arr;
    }

    static void Main()
    {
        int size = 10;

        char[] randomChars = GenerateRandomLowercaseArray(size);

        Console.WriteLine("Random lowercase characters:");
        Console.WriteLine(new string(randomChars));
    }
}


/*
run:

Random lowercase characters:
ewpniwmwuj

*/

 



answered Apr 25 by avibootz
0 votes
using System;

class Program
{
    // Generates random alphabetic characters (uppercase + lowercase)
    static char[] GenerateRandomAlphabetArray(int length)
    {
        char[] arr = new char[length];
        Random rand = new Random();

        for (int i = 0; i < length; i++)
        {
            int choice = rand.Next(2); // 0 = uppercase, 1 = lowercase

            if (choice == 0)
                arr[i] = (char)('A' + rand.Next(26));
            else
                arr[i] = (char)('a' + rand.Next(26));
        }

        return arr;
    }

    static void Main()
    {
        int size = 10;

        char[] randomChars = GenerateRandomAlphabetArray(size);

        Console.WriteLine("Random alphabetic characters:");
        Console.WriteLine(new string(randomChars));
    }
}


/*
run:

Random alphabetic characters:
mgKpQBjMlq

*/

 



answered Apr 25 by avibootz
0 votes
using System;

class Program
{
    // Generates random printable ASCII characters
    static char[] GenerateRandomAsciiArray(int length)
    {
        char[] arr = new char[length];
        Random rand = new Random();

        for (int i = 0; i < length; i++) {
            // Printable ASCII characters: 32 to 126
            arr[i] = (char)rand.Next(32, 127);
        }

        return arr;
    }

    static void Main()
    {
        int size = 10;

        char[] randomChars = GenerateRandomAsciiArray(size);

        Console.WriteLine("Random ASCII characters:");
        Console.WriteLine(new string(randomChars));
    }
}


/*
run:

Random ASCII characters:
j%Uve=o%t6

*/

 



answered Apr 25 by avibootz

Related questions

1 answer 71 views
1 answer 92 views
1 answer 99 views
1 answer 108 views
1 answer 91 views
2 answers 109 views
...