Home > database >  how to take a random element out of an array in C#
how to take a random element out of an array in C#

Time:12-25

I am trying to take out a random element from my array in the following code:

                char[] vs = new char[] { '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', ' ', '=', '|', ':', };
                string strng = null;

                Console.WriteLine(vs.Length);
                while (u <= 536870912)
                {
                    Random random = new Random();
                    random.Next(0, 51);
                    int rndm = Convert.ToInt32(random);
                    strng = strng   ""   vs[rndm];

                }
                Console.WriteLine(strng);

so whenever i try to run it it gives me an unhandled exception, here it is:

52
System.InvalidCastException: Unable to cast object of type 'System.Random' to type 'System.IConvertible'.
   at System.Convert.ToInt32(Object value)
   at ConsoleApp1.Program.Main()

the main purpose of the whole code is to generate random characters from the array, as long as it's less than 512 megabytes (which can be noticed in the code), if you have any idea please help me figure this out, thanks!

CodePudding user response:

You should store the return value of the random.Next() method into a variable. (The problem is that you try to cast/convert the class instance to an int.)

using System;
                    
public class Program
{
    public static void Main()
    {
        int u=0;
        
        char[] vs = new char[] { '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', ' ', '=', '|', ':', };
        string strng = null;

        Console.WriteLine(vs.Length);
        // just create an instance of the Random class once.
        // the "DateTime.Now.Millisecond" is a seed, so it
        // won't select the same random sequence.
        Random random = new Random(DateTime.Now.Millisecond);

        while (u <= 10)
        {
            // store the random value into rndm (it's already an int)
            var rndm = random.Next(0, vs.Length);
            strng = strng   vs[rndm];
            u  ;
        }
        Console.WriteLine(strng);
    }
}

CodePudding user response:

var element = arr[new Random().next(0,51)]; for your code:

char[] vs = new char[] { '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', ' ', '=', '|', ':', };
                string strng = null;

                Console.WriteLine(vs.Length);
                while (u <= 536870912)
                {
                    Random random = new Random(DateTime.Now.Millisecond);
                    int rndm = random.Next(0, 51);
                    strng = strng   ""   vs[rndm];

                }
                Console.WriteLine(strng);
  • Related