Several new features have been added to the board, check the Announcements for more details!

How to Create a Random Number Generator (Part 1)

Here you can learn how to do many things on the computer or a programming environment.
User avatar

Topic Author
AVenger
Board Moderators
Board Moderators
Posts: 546
Joined: 11 Aug 2011, 19:02
Tech Knowledge: Advanced
Location: Maryland
Contact:

How to Create a Random Number Generator (Part 1)

Postby AVenger » 17 Apr 2012, 00:53

In this [b]How To[/b], I will show you how to create a random number generator from scratch in Visual Studio 2010 in the Visual C# programming language. You can download Visual C# Express for free at the Microsoft website.

Step 1: The first thing to do is start up Visual Studio or Visual C# Express.

Step 2: Then you need to create a new project,
[quote]Image[/quote]
and click "Console Application",
[quote]Image[/quote]
and name it something you would name a random number generator.
[quote]Image[/quote]

Step 3: Once you get inside the programming environment, you will see some pre-code. When coding a simple program, just place your code inside the "Main" code bars.
[quote]Image[/quote]

Step 4: The first step is to create a couple of variables so we can add values to them later. Insert this code inside the "Main" brackets.
[code]string number = "";
int num = 0;
int randomNum = 0;[/code]

Step 5: The next step is to ask the user for a number that you can use for the maximum number.
[code]Console.WriteLine("Enter the maximum number you would like this generator to generate up to:");
Console.Write("> ");[/code]
With this code, we use the "Console.WriteLine" to print out the statement to enter in the number. Then we use the "Console.Write" to let the user enter in their number. "WriteLine" prints out the statement and then goes to the next line. "Write" just prints out the statement without going to the next line.

Step 6: Now we need to read what the user enters. Put this code below the last code.
[code]number = Console.ReadLine();[/code]
All this does is put what the user enters ("Console.ReadLine") into the variable "number."

Step 7: Next, we need to convert that string value to an integer value so we can use it in the random statements. Insert this code below the last statements.
[code]num = Convert.ToInt32(number);[/code]

Step 8: The next step is to create a random number from the number they entered. First, we need to create a new link between the "Random" class and our project. Insert this code.
[code]Random ran = new Random();[/code]
Once we have that, then we can use "ran" to access all the commands inside the "Random" class. Use this code to generate a random number.
[code]randomNum = ran.Next(1, num);[/code]
Inside the "Next" parentheses there is the 1 and the "num." The 1 is the lowest number that can be generated and the "num" is the highest number that can be generated.

Step 9: Finally, we just print out the results.
[code]Console.WriteLine("Your random number is: {0}", randomNum);
Console.ReadLine();[/code]
Using the "{0}", we are able to put that inside the quotation marks instead of using concatenation like this.
[code]Console.WriteLine("Your random number is: " + randomNum);[/code]
In this particular case, either is easy; but in longer statements, it is easier to use "{0}" than concatenation.
Then we use "Console.ReadLine()" to wait for the enter button to be pressed and then exits the program.

When it is all done, this is what we see:
[quote]Image
Image
Image[/quote]

That is a simple random number generator. More tutorials will be posted going into more detail on my advanced random number generator.

Return to “How-tos”

Who is online

Users browsing this forum: No registered users and 1 guest

cron