Random Code Generator using C#
Submitted by donbermoy on Thursday, December 10, 2015 - 17:15.
In this tutorial, I will teach you how to create a program that will generate a random code or id using the C# language.
So, now let's start this tutorial!
1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application.
2. Add one textbox named txtIDno and one button named btnGenerate.
3. Now, create a function named generateCode. Have this code below.
Lastly, put your function in your button.
Output:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

- void generateCode()
- {
- //Initialize variable x as an empty string and a rand varaiable as Random
- String x = "";
- // The z string variable holds the current year
- string z = Convert.ToString(DateTime.Now.Year);
- // This loop creates 2 random numbers at first and concatenates the STI word after the 2 random numbers
- // And also it only generates a number from 0-9
- for (int y = 1; y <= 2; y++)
- {
- x += Convert.ToString(rand.Next(0, 9));
- if (y == 2)
- {
- x += "STI-";
- }
- }
- // This loop creates 3 random numbers from 0-9
- for (int y = 1; y <= 3; y++)
- {
- x += Convert.ToString(rand.Next(0, 9));
- }
- //display the generated code in the textbox
- txtIDno.Text = z + "-" + x;
- }
- private void btnGenerate_Click(object sender, EventArgs e)
- {
- if (txtIDno.Text == "")
- {
- generateCode();
- }
- }

Add new comment
- 128 views