Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Generating numbers and letters in a formatted string.

Hello it's time to drop a thread about generating your own license keys for your own services and or generating a string to a specific format. I'll explain the process with comments in code.

Code
       static void Main(string[] args)
       {

           
           string[] letters = { "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" }; //this is defining letters in an array I will be converting this ToUpper() for this example.
           
           
           Random r = new Random(); //instantiating the Random() class and assigning it's name r. This class is used for generating random numbers.
           
               
                  // these lines below is making an implicit variable (var) that will be passed along the string array "letters" and using the random "Next" method identifier (r.Next) we can generate between certain numbers it'll be using for the array. If I wanted to find a random number between 0-10, you'd do r.Next(0,10) but for this example were passing in the array of letters which max would be 26, so 0-25 would be used since arrays start at position 0, not 1.
                   var _letter1 = (letters[r.Next(0, 25)]);
                   var _letter2 = (letters[r.Next(0, 25)]);
                   var _letter3 = (letters[r.Next(0, 25)]);
                   var _letter4 = (letters[r.Next(0, 25)]);
                   var _letter5 = (letters[r.Next(0, 25)]);
                   var _letter6 = (letters [r.Next(0, 25)]);
                   var _1 = r.Next(0, 9);
                   var _2 = r.Next(0, 9);
                   var _3 = r.Next(0, 9);
                   string dash = "-";
                   
                   //if we wanted to create the format: ABC-123-ABC and inserting random values to this format we'd do.

                   var result = (_letter1 + _letter2 + _letter3 + dash + _1 + _2 + _3 + dash + _letter4 + _letter5 + _letter6).ToString().ToUpper();
                   
                    //you could also do it without the dash variable and just use "-" and I'm also converting to a string and invoking the ToUpper() method for all capital letters.
                   Console.WriteLine(result);
                   Console.ReadLine();

       }

A good way to use this is to put it into a loop and it'll generate different values each time.
Use a char array instead of a string array and make all characters uppercase. That way you can remove the ToUpper call.

Other than that, solid contribution.
[Image: vp6iypF.gif]
(Oct 24, 2019 05:27 PM)MavenGradle Wrote:  Use a char array instead of a string array and make all characters uppercase. That way you can remove the ToUpper call.

Other than that, solid contribution.

Yes I missed that, I'll change the array type thank you. And I'm leaving in the conversion since I explained it's part of the process in my example. :)
(Oct 24, 2019 05:32 PM)Pawnhub Wrote:  Yes I missed that, I'll change the array type thank you. And I'm leaving in the conversion since I explained it's part of the process in my example. :)

You can completely get rid of the integer array since the random generator can generate numbers for you:
Code
r.Next(10);
[Image: vp6iypF.gif]
(Oct 24, 2019 05:34 PM)MavenGradle Wrote:  You can completely get rid of the integer array since the random generator can generate numbers for you:
Code
r.Next(10);

Again thank you maven, I have fixed that as well. :)

(Oct 24, 2019 05:27 PM)MavenGradle Wrote:  Use a char array instead of a string array and make all characters uppercase. That way you can remove the ToUpper call.

Other than that, solid contribution.

Update on the char array, you might want to test the code but it's not outputting correctly. Do you know why? Until then it's a string array.
I wouldn't call this a solid contribution, it doesn't even work as designed.

Random Next() - https://docs.microsoft.com/en-us/dotnet/...tem_Int32_
Quote:Returns
Int32
A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.

The code doesn't follow standard naming conventions, and for a fixed length output you could have used a char array of the specified length to convert to a string rather than concatenating variables. The variable use demonstrates that you haven't understood how to apply the concept of arrays where it makes sense to do so. You also have a char array defined using string values as elements.

The ToString() call is also redundant: In order to concatenate an int and a string, an implicit ToString() is called behind the scenes so the value you're calling ToString() on explicitly is already a string...

You don't really need any array for the characters from A-Z if you took the time to understand the ASCII table.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Oct 24, 2019 07:42 PM)0xDEAD10CC Wrote:  I wouldn't call this a solid contribution, it doesn't even work as designed.

Random Next() - Random.Next Method (System) | Microsoft Docs

The code doesn't follow standard naming conventions, and for a fixed length output you could have used a char array of the specified length to convert to a string rather than concatenating variables. The variable use demonstrates that you haven't understood how to apply the concept of arrays where it makes sense to do so. You also have a char array defined using string values as elements.

The ToString() call is also redundant: In order to concatenate an int and a string, an implicit ToString() is called behind the scenes so the value you're calling ToString() on explicitly is already a string...

You don't really need any array for the characters from A-Z if you took the time to understand the ASCII table.

I see where I went wrong thanks. Can't update thread since I've updated this alot.
Code
static void Main(string[] args)
{
    var rnd = new Random();
    var arr = new char[10]; // probably don't care about the magic numbers
                            // that follow for such a small program.

    arr[6] = '-';
    for (int i = 0; i < 6; i++) arr[i] = 'A' + r.Next(26);
    for (int i = 7; i < 10; i++) arr[i] = '0' + r.Next(10);
    
    var result = new string(arr);

    Console.WriteLine(result);
    Console.ReadLine();
}
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Oct 24, 2019 08:19 PM)0xDEAD10CC Wrote:  
Code
static void Main(string[] args)
{
   var rnd = new Random();
   var arr = new char[10]; // probably don't care about the magic numbers
                           // that follow for such a small program.

   arr[6] = '-';
   for (int i = 0; i < 6; i++) arr[i] = 'A' + r.Next(26);
   for (int i = 7; i < 10; i++) arr[i] = '0' + r.Next(10);
   
   var result = new string(arr);

   Console.WriteLine(result);
   Console.ReadLine();
}
Yeah I can see how this would be better.

Question if using random(0,9) normally to print a integer it won't print 9 from the quote you posted earlier?

But if it's enumerating through an array for example this array like so
Code
(letters[r.Next(0, 26)]);
I still get Z as an output why does this happen if it don't count the max value or am I misunderstanding something? It also doesn't call an index exception. very confused
(Oct 24, 2019 08:25 PM)Pawnhub Wrote:  Yeah I can see how this would be better.

Question if using random(0,9) normally to print a integer it won't print 9 from the quote you posted earlier?

But if it's enumerating through an array for example this array like so
Code
(letters[r.Next(0, 26)]);
I still get Z as an output why does this happen if it don't count the max value or am I misunderstanding something? It also doesn't call an index exception. very confused

Read it again... Max is exclusive, so how would it return 9?

Your original code used (0, 9) and (0, 25) which means 9 and 25 won't be returned.

You get Z using (0, 26) because 0..25 inclusive is 26 values...
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Oct 24, 2019 08:36 PM)0xDEAD10CC Wrote:  Read it again... Max is exclusive, so how would it return 9?

I'm talking about the string array above. I see when doing a normal Random.Next(0,9) will return max 8.
But the alphabet array I made is 26 chars long, when enumerating through the array, random still uses the last value (26 = Z) in (letters[r.Next(0, 26)]);

(Oct 24, 2019 08:36 PM)0xDEAD10CC Wrote:  You get Z using (0, 26) because 0..25 inclusive is 26 values...

Oh alright this is starting to make sense I think?
(Oct 24, 2019 08:39 PM)Pawnhub Wrote:  I'm talking about the string array above. I see when doing a normal Random.Next(0,9) will return max 8.
But the alphabet array I made is 26 chars long, when enumerating through the array, random still uses the last value (26 = Z) in (letters[r.Next(0, 26)]);

You clearly don't understand array indexing... [0] = A, [25] = Z (not index [26])

Next(0, 26) will return values from 0 through 25. Index [25] is the 26th value because index [0] is the first element, not [1].
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Oct 24, 2019 08:42 PM)0xDEAD10CC Wrote:  You clearly don't understand array indexing... [0] = A, [25] = Z  (not index [26])

Next(0, 26) will return values from 0 through 25.

🤦‍♂ I get it now. I see what happened.
I'm also calling the overload with one parameter:
Code
[Random].Next(Int32)

But the following two calls are functionally equivalent:
Code
rnd.Next(0, 26);

Code
rnd.Next(26);
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Oct 24, 2019 08:19 PM)0xDEAD10CC Wrote:  
Code
static void Main(string[] args)
{
   var rnd = new Random();
   var arr = new char[10]; // probably don't care about the magic numbers
                           // that follow for such a small program.

   arr[6] = '-';
   for (int i = 0; i < 6; i++) arr[i] = 'A' + r.Next(26);
   for (int i = 7; i < 10; i++) arr[i] = '0' + r.Next(10);
   
   var result = new string(arr);

   Console.WriteLine(result);
   Console.ReadLine();
}

For all intents and purposes you forgot the cast and used a wrong random variable:
Code
for (int i = 0; i < 6; i++) arr[i] = (char) ('A' + rnd.Next(26));
for (int i = 7; i < 10; i++) arr[i] = (char) ('0' + rnd.Next(10));

This is how it could look like in relation to the format OP used:
Code
           Random rnd = new Random();
           char[] arr = new char[11];
           const char dash = '-';
           arr[3] = dash;
           arr[7] = dash;
           for (int i = 0; i < 3; i++) arr[i] = (char) ('A' + rnd.Next(26));
           for (int i = 4; i < 7; i++) arr[i] = (char) ('0' + rnd.Next(10));
           for (int i = 8; i < 11; i++) arr[i] = (char) ('A' + rnd.Next(26));
           string result = new string(arr);

Or
Code
           Random rnd = new Random();
           char[] arr =
           {
               (char) ('A' + rnd.Next(26)), (char) ('A' + rnd.Next(26)), (char) ('A' + rnd.Next(26)), '-',
               (char) ('0' + rnd.Next(10)), (char) ('0' + rnd.Next(10)), (char) ('0' + rnd.Next(10)), '-',
               (char) ('A' + rnd.Next(26)), (char) ('A' + rnd.Next(26)), (char) ('A' + rnd.Next(26))
           };
           string result = new string(arr);
if you prefer not to loop. This is harder to maintain in case format changes though.

If you prefer interpolation (degrades performance significantly):
Code
       private static readonly Random Random = new Random();
       static void Main(string[] args)
       {
           Console.WriteLine($"{GetRandomChar()}{GetRandomChar()}{GetRandomChar()}-{GetRandomNumber()}{GetRandomNumber()}{GetRandomNumber()}-{GetRandomChar()}{GetRandomChar()}{GetRandomChar()}");
       }
       private static char GetRandomChar() => (char) ('A' + Random.Next(26));
       private static char GetRandomNumber() => (char) ('0' + Random.Next(10));
^ All of the above should be modified to adhere to OOP standards.
[Image: vp6iypF.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by absojediDec 27, 2020 02:43 PM 3 139 Dec 27, 2020 02:43 PM
Last Post: absojedi
[Java] by .eXeSep 16, 2020 03:39 AM 3 223 Sep 16, 2020 03:39 AM
Last Post: InterestingUser1111
[C] by MordiciaNov 15, 2019 07:21 PM 3 210 Nov 15, 2019 07:21 PM
Last Post: Mordicia