0

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

1 Answers1

1

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

Atk
  • 754
  • 1
  • 4
  • 12
  • That's exactly what I needed, but how would I loop that so it generates the keys unlimited times, or is it possible using a function to ask how many I wanna generate and generate that many? – Deadly Famous Feb 01 '20 at 05:40
  • use Console.ReadLine() to read user inputted string and replace that number in the loop iteration. – Atk Feb 01 '20 at 05:43
  • 1
    @DeadlyFamous, 16 octets of a generated GUID are represented as 32 hexadecimal digits. Hexadecimal digits are characters 0-9 and A-F. So it would not fully satisfy your requirements to have other letters as well (such as "ZWN6QV9Z"). – Fabio Feb 01 '20 at 06:23
  • @DeadlyFamous - And using `Substring` on the string representation of a guid is not guaranteed to be unique! – Chris Dunaway Feb 03 '20 at 15:02