ยฉ 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#โ€ข4y agoโ€ข
12 replies
cookiesaredelicious

โœ… Random character generator

Hello, as a beginner, I am currently working on making a simple lottery simulator. However, I didn't know how to generate a sequence of 8 letters and numbers. Naturally, I searched it up and I got the answer. But copying chunks of code wouldn't help me learn and I can't seem to wrap my head around how this works. I understand the variables, but I can't just seem to understand what the variable i does and why i = 0. Would anyone be kind enough to explain the for statement and what each argument does. Thank you so much ๐Ÿ™

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var stringChars = new char[8];
            var randomNumber = new Random();

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

            var finalString = new String(stringChars);

            Console.WriteLine(finalString);
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var stringChars = new char[8];
            var randomNumber = new Random();

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

            var finalString = new String(stringChars);

            Console.WriteLine(finalString);
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Random Generator method
C#CC# / help
2y ago
Random Name generator.
C#CC# / help
3y ago
โ” โœ… Semi-Random-Generator
C#CC# / help
4y ago