Some of the systems and applications today are using a Random Number Generator to generate random id number which will be used for their id in the database or let's just say an application by guessing a number. So in this tutorial, I will teach you how to create a program that generates a random number using C#.
So, now let's start making this program!
1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to
File, click
New, and choose
Windows Form Application and name your project as
Random Number Generator.
2. Next, add one button and named
Button1. Have it also labeled as "
Create Random Number". Follow this designed interface:
3. Next, in the code tab, create a code for your Button1_Click. Type this code below:
public Random random
= new Random
(); public void Button1_Click(System.Object sender, System.EventArgs e)
{
int i = default(int);
for (i = 1; i <= 3; i++)
{
MessageBox.Show(Convert.ToString(random.Next(0, 1000)));
}
}
We declare
public Random random = new Random();
as our Global Variable for instantiating a Random Object new Random(); that holds the variable
random. We also initialized Dim
i As Integer as our integer so that we can generate three random numbers
(For i=1 to 3) when we click OK button in the MsgBox.
MessageBox.Show(Convert.ToString(random.Next(0, 1000)));
syntax is for displaying the random number. random.Next(0, 1000) initializes a random number from 0 to 1000, then the output is converted into a string using
Convert.ToString syntax.
Now, click your Button. It will now generate random number like this:
Click
OK button in the MessageBox again. It will generate again a random number like this:
Lastly, click the
OK button. It will generate again a random number like this:
We can only generate random number three times as we conditioned
For i = 1 to 3.
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