![]() |
|
[C#] Generating numbers and letters in a formatted string. - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Generating numbers and letters in a formatted string. (/showthread.php?tid=6019165) Pages: Pages
1
2
|
Generating numbers and letters in a formatted string. - Pawnhub - Oct 24, 2019 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)A good way to use this is to put it into a loop and it'll generate different values each time. RE: Generating numbers and or letters in a formatted string. - MavenGradle - Oct 24, 2019 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. RE: Generating numbers and or letters in a formatted string. - Pawnhub - Oct 24, 2019 (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. 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. :) RE: Generating numbers and or letters in a formatted string. - MavenGradle - Oct 24, 2019 (Oct 24, 2019 05:32 PM)UnknownPawn 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);RE: Generating numbers and or letters in a formatted string. - Pawnhub - Oct 24, 2019 (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: 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. 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. RE: Generating numbers and letters in a formatted string. - 0xDEAD10CC - Oct 24, 2019 I wouldn't call this a solid contribution, it doesn't even work as designed. Random Next() - https://docs.microsoft.com/en-us/dotnet/api/system.random.next?view=netframework-4.8#System_Random_Next_System_Int32_System_Int32_ Quote:Returns 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. RE: Generating numbers and letters in a formatted string. - Pawnhub - Oct 24, 2019 (Oct 24, 2019 07:42 PM)0xDEAD10CC Wrote: I wouldn't call this a solid contribution, it doesn't even work as designed. I see where I went wrong thanks. Can't update thread since I've updated this alot. RE: Generating numbers and letters in a formatted string. - 0xDEAD10CC - Oct 24, 2019 Code static void Main(string[] args)RE: Generating numbers and letters in a formatted string. - Pawnhub - Oct 24, 2019 (Oct 24, 2019 08:19 PM)0xDEAD10CC 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)]);RE: Generating numbers and letters in a formatted string. - 0xDEAD10CC - Oct 24, 2019 (Oct 24, 2019 08:25 PM)UnknownPawn Wrote: Yeah I can see how this would be better. 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... RE: Generating numbers and letters in a formatted string. - Pawnhub - Oct 24, 2019 (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? RE: Generating numbers and letters in a formatted string. - 0xDEAD10CC - Oct 24, 2019 (Oct 24, 2019 08:39 PM)UnknownPawn Wrote: I'm talking about the string array above. I see when doing a normal Random.Next(0,9) will return max 8. 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]. RE: Generating numbers and letters in a formatted string. - Pawnhub - Oct 24, 2019 (Oct 24, 2019 08:42 PM)0xDEAD10CC Wrote: You clearly don't understand array indexing... [0] = A, [25] = Z (not index [26]) 🤦♂ I get it now. I see what happened. RE: Generating numbers and letters in a formatted string. - 0xDEAD10CC - Oct 24, 2019 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);RE: Generating numbers and letters in a formatted string. - MavenGradle - Oct 25, 2019 (Oct 24, 2019 08:19 PM)0xDEAD10CC Wrote: 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));This is how it could look like in relation to the format OP used: Code Random rnd = new Random();Or Code Random rnd = new Random();If you prefer interpolation (degrades performance significantly): Code private static readonly Random Random = new Random(); |