简体   繁体   中英

VB.net C# number and letter generator

I been having issues creating a number and letter generator, it should look like this: 9WJLNN8MNDVJCFLQJ4W93YH6ZM:ZWN6QV9ZXG9YCMWAXXWP492DS9

26 letters and numbers randomly colon and same thing after colon, but I keep getting errors but heres my code from what I got so far Right now I cant even get the numbers and letters together to make it work, im just so confused on what to do. If anyone can help me out that would be amazing. I've been working on this for a few days now.

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;

namespace Testing23891721983712983981
{
    class Program
    {
        static void Main(string[] args)
        {

            {
                Random rand = new Random();
                int[] numbers = new int[4];
                for (int i = 0; i < 4; i++)
                {
                    numbers[i] = rand.Next(1000, 10000);
                }

                string prefix = string.Join("-", numbers);

                for (int i = 0; i < 100; i++)
                {
                    int threeDigits = rand.Next(100, 1000);



                    RandomGenerator generator = new RandomGenerator();



                    string str = generator.RandomString(26, false);
                    Console.WriteLine(threeDigits, str);

                    Console.ReadKey();


                }
            }
        }

        public class RandomGenerator
        {
            // Generate a random number between two numbers    
            public int RandomNumber(int min, int max)
            {
                Random random = new Random();
                return random.Next(min, max);
            }

            // Generate a random string with a given size    
            public string RandomString(int size, bool lowerCase)
            {
                StringBuilder builder = new StringBuilder();
                Random random = new Random();
                char ch;
                for (int i = 0; i < size; i++)
                {
                    ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                    builder.Append(ch);
                }
                if (lowerCase)
                    return builder.ToString().ToLower();
                return builder.ToString();
            }

            // Generate a random password    
            public string RandomPassword()
            {
                StringBuilder builder = new StringBuilder();
                builder.Append(RandomString(4, true));
                builder.Append(RandomNumber(1000, 9999));
                builder.Append(RandomString(2, false));
                return builder.ToString();
            }
        }
    }
}

use Guid class to generate random numbers with string and then use substring on it.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp4
    {
    class Program
        {
        static void Main(string[] args)
           {
               {
                Random rand = new Random();
                int[] numbers = new int[4];
                for (int i = 0; i < 4; i++)
                {
                    numbers[i] = rand.Next(1000, 10000);
                }

                string prefix = string.Join("-", numbers);
                string strguid = "";

                for (int i = 0; i < 2; i++)
                {
                    Guid guid = Guid.NewGuid();

                    if (strguid != "")
                    {
                        strguid = strguid + ":" + guid.ToString().Replace("-", "").Substring(0, 26).ToUpper();
                    }
                    else
                    {
                        strguid = guid.ToString().Replace("-", "").Substring(0, 26).ToUpper();
                    }


                }


                Console.WriteLine(strguid);

                Console.ReadKey();
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM