-1

How to add like number or character pattern to my random keygen? and is it hard becuse im new to coding :) Thx for Help! it took me alot of time to get to this and been stuck here for 1 and half day and can't find way to add patterns to this

Like This :

D4B6C5604E26-4F1198-44C1
EA3705694B8A-478E83-2D01
D3B8E2DE7BFC-49CF95-68E6
A6CD996B352A-48B89A-8C69

After - 4 Numbers and After second - 3 Numbers

static void Main(string[] args)
{
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    var stringChars = new char[12];
    var stringChars4 = new char[6];
    var stringChars7 = new char[4];
    var random = new Random();

    for (int i = 0; i < stringChars.Length; i++)
    {
        stringChars[i] = chars[random.Next(chars.Length)];
    }

    for (int i = 0; i < stringChars4.Length; i++)
    {
        stringChars4[i] = chars[random.Next(chars.Length)];
    }

    for (int i = 0; i < stringChars7.Length; i++)
    {
        stringChars7[i] = chars[random.Next(chars.Length)];
    }
    var finalString = new String(stringChars);
    var finalString4 = new String(stringChars4);
    var finalString7 = new String(stringChars7);

    var chars2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    var stringChars2 = new char[12];
    var stringChars5 = new char[6];
    var stringChars8 = new char[4];
    var randoms = new Random();

    for (int i = 0; i < stringChars.Length; i++)
    {
        stringChars2[i] = chars2[random.Next(chars.Length)];
    }

    for (int i = 0; i < stringChars5.Length; i++)
    {
        stringChars5[i] = chars2[random.Next(chars.Length)];
    }

    for (int i = 0; i < stringChars8.Length; i++)
    {
        stringChars8[i] = chars2[random.Next(chars.Length)];
    }

    var finalString2 = new String(stringChars2);
    var finalString8 = new String(stringChars8);
    var finalString5 = new String(stringChars5);

    var chars3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    var stringChars3 = new char[12];
    var stringChars6 = new char[6];
    var stringChars9 = new char[4];
    var randomss = new Random();

    for (int i = 0; i < stringChars3.Length; i++)
    {
        stringChars3[i] = chars3[random.Next(chars3.Length)];
    }

    for (int i = 0; i < stringChars6.Length; i++)
    {
        stringChars6[i] = chars3[random.Next(chars3.Length)];
    }

    for (int i = 0; i < stringChars9.Length; i++)
    {
        stringChars9[i] = chars3[random.Next(chars3.Length)];
    }

    var finalString3 = new String(stringChars3);
    var finalString6 = new String(stringChars6);
    var finalString9 = new String(stringChars9);

    Console.WriteLine("Keys:");
    Console.WriteLine();
    Console.ReadKey();
    Console.WriteLine(finalString + "-" + finalString4 + "-" + finalString7);
    Console.WriteLine();
    Console.ReadKey();
    Console.WriteLine(finalString2 + "-" + finalString5 + "-" + finalString8);
    Console.WriteLine();
    Console.ReadKey();
    Console.WriteLine(finalString3 + "-" + finalString6 + "-" + finalString9);
    Console.WriteLine();
    Console.ReadKey();
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Rapsu
  • 13
  • 2
  • *Random* is not the same as *unique* which is what it sounds like you are trying for. perhaps research the term ***GUID*** and see if that might work for you. – Ňɏssa Pøngjǣrdenlarp May 18 '19 at 00:06
  • 1
    Hi Rapsu, it is not clear what exactly you are looking for. For example, what do you mean by "After - 4 Numbers and After second - 3 Numbers"? Also, I would suggest reading up on [StringBuilder](https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder), specifically the remarks section. – Trisped May 18 '19 at 00:17
  • 1
    You've posted your desired results and code, but you didn't mention in what way the code fails to meet your needs. A _clear problem statement_ would help us help you. – HABO May 18 '19 at 00:40

2 Answers2

0

Create a function for the code generation, it makes the main method more readable.

private static readonly Random _random = new Random();

private static string CreateCode()
{
    var bytes = new byte[11];
    _random.NextBytes(bytes);
    string s = BitConverter.ToString(bytes).Replace("-", "");
    string result = new StringBuilder(s)
        .Insert(18, '-')
        .Insert(12, '-')
        .ToString();
    return result;
}

static void Main(string[] args)
{
    const int N = 3;

    var codes = new string[N];
    for (int i = 0; i < N; i++) {
        codes[i] = CreateCode();
        Console.WriteLine(codes[i]);
    }
}

I use the Random.NextBytes method to generate random bytes. We need 11 of them, because one byte is represented by 2 hex positions.

Your codes are in hexadecimal format, i,e, they contain only the letters A - F and digits. This solution uses the BitConverter to format a byte array as hexadecimal string. It produces strings like ""BD-EB-1F-0C-9B-9E-0C-F5-6E-2E-46". Therefore it is necessary to remove the "-" first.

Then I convert the string into a StringBuilder. The latter one has a Insert method that we can use to insert dashes at the required places. I insert the second one first, so that the index of the other one is not shifted.

You could also simply call Console.WriteLine(CreateCode()); three times and not create the codes array. But if you want to do other things with the codes, like saving them to a file or copy them to the cilpboard, it's better to store them somewhere.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

Assuming you are looking for "get string of random characters (from given set of characters) that formatted to given specification like 'xxx-xx-xxxx!xxx' where 'x' is random character".

Regex.Replace is a nice way to construct such string - it let you run arbitrary code to construct replacement - so replacing every 'x' with randomly selected character will produce result you seem to be looking for:

var r = new Random();
// convert string to array of strings for individual characters as Replace wants strings 
var charsAsStrings = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        .Select(x=>x.ToString()).ToArray();

var result = Regex.Replace("xxx-xxx", "x", 
      m => charsAsStrings[r.Next(charsAsStrings.Length)]));

Notes:

  • make sure to read Random number generator only generating one random number to properly instantiate Random.
  • random numbers/strings are not unique. Presumably you will store them in some sort of list/database and re-generate the once that are not unique
  • using similarly-looking symbols like 'O' and '0' (or 'I', 'l','1') in strings that may need to be read by humans is not the best idea.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179