You are on page 1of 6

5/6/2020 How to Generate Random Numbers in .

NET | Webucator

Course Catalog Training Options

Home (https://www.webucato… /  How-to Articles (https://ww… /  How to Generate Random …

 

How to Generate Random Numbers in .NET


The steps below describe how to generate random numbers in .NET.

To generate random numbers in a .Net application, we use the framework class "Random". Random is

in the mscorlib library so there is no need to bring any additional namespaces into scope. The
"Random" class provides ve methods used to generate various types of random number.

1. To begin, we Instantiate the Random Class. The class has two constructor overloads. One is the
default constructor (receives no parameters) which uses a time-dependent default seed value to
initialize the class. The second constructor receives an Int32 seed value which is used to initialize
the class.
C#

//Create an instance of the Random class


//Using the Default Constructor
Random r = new Random();
//Or Passing in a seed value
Random r2 = new Random(65);

VB.Net

https://www.webucator.com/how-to/how-generate-random-numbers-net.cfm 1/6
5/6/2020 How to Generate Random Numbers in .NET | Webucator

'Create an instance of the Random class


'Using the Default Constructor
Dim r As New Random()
'Or Passing in a seed value
Dim r2 As New Random(65)

2. Next we declare some variables to hold the results of the various method calls.
C#

//declare variables to hold the result


int resultInt;
double resultDouble;
byte[] resultBytes = new byte[12];

VB.Net

'declare variables to hold the result


Dim resultInt As Integer
Dim resultDouble As Double
Dim resultBytes(12) As Byte

3. Finally, we exercise each of the methods and retrieve the results


C#

//return a nonnegative random integer


resultInt = r.Next();

//return a random integer between 50 and 100


resultInt = r.Next(50, 100);

//return a nonnegative integer that is less than 2000


resultInt = r.Next(2000);

//return a random floating point integer betweem 0.00 and 1.00


resultDouble= r.NextDouble();

//Fill the array with random numbers


r.NextBytes(resultBytes);

VB.Net

https://www.webucator.com/how-to/how-generate-random-numbers-net.cfm 2/6
5/6/2020 How to Generate Random Numbers in .NET | Webucator

'return a nonnegative random integer


resultInt = r.Next()

'return a random integer between 50 and 100


resultInt = r.Next(50, 100)

'return a nonnegative integer that is less than 2000


resultInt = r.Next(2000)

'return a random floating point integer betweem 0.00 and 1.00


resultDouble = r.NextDouble()

'Fill the array with random numbers


r.NextBytes(resultBytes)

4. Be aware the Random class is a "psuedo random number generator". In other words, it is not truly
random. The "randomness" does, however, meet certain statistical requirements for random
numbers.

Author: Michael Osborne

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have

trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft

ASP.NET, Microsoft O ce, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and

much more. Check out our complete course catalog

(https://www.webucator.com/course/catalog.cfm).

WEBUCATOR DELIVERS INSTRUCTOR-LED AND SELF-PACED TRAINING

Microsoft Training(https://www.webucator.com/microsoft-training/index.cfm)

Database Training
https://www.webucator.com/how-to/how-generate-random-numbers-net.cfm 3/6
5/6/2020 How to Generate Random Numbers in .NET | Webucator

(https://www.webucator.com/database-training/index.cfm)

Web Development Training(https://www.webucator.com/webdev-training/index.cfm)

Java Training(https://www.webucator.com/java-training/index.cfm)

Adobe Training(https://www.webucator.com/adobe-training/index.cfm)

Programming Training(https://www.webucator.com/programming-training/index.cfm)

Big Data Training(https://www.webucator.com/big-data-training/index.cfm)

Cloud Training(https://www.webucator.com/cloud-training/index.cfm)

Business Skills Training(https://www.webucator.com/business-skills-training/index.cfm)

Web Application Server Training


(https://www.webucator.com/web-application-servers-training/index.cfm)

NEED ASP.NET TRAINING?

Webucator provides instructor-led online and onsite training. Check out our complete course catalog
(https://www.webucator.com/course/catalog.cfm).

ASP.NET Courses(https://www.webucator.com/microsoft-training/aspnet-training.cfm)

.NET Core Frameworks


(https://www.webucator.com/microsoft-training/course/net-core-frameworks.cfm)

https://www.webucator.com/how-to/how-generate-random-numbers-net.cfm 4/6
5/6/2020 How to Generate Random Numbers in .NET | Webucator

0 Comments Webucator How-Tos 🔒 Disqus' Privacy Policy 


1 Login

 Recommend t Tweet f Share Sort by Best

Start the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Be the first to comment.

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd ⚠ Do Not Sell My Data

Live Online (https://www.webucator.com/live-online-training/index.cfm) 

Self-paced (https://www.webucator.com/self-paced-training/index.cfm) 

Customized Onsite (https://www.webucator.com/customized-private-training/index.cfm) 

Enterprise Solutions (https://www.webucator.com/enterprise-training/index.cfm) 

All Training Options (https://www.webucator.com/about-us/training-options.cfm) 

Full Catalog (https://www.webucator.com/course/catalog.cfm) 

Pricing & Savings (https://www.webucator.com/about-us/pricing.cfm) 

Why Webucator (https://www.webucator.com/about-us/compare-us.cfm) 

Testimonials (https://www.webucator.com/about-us/testimonials.cfm) 

Client List (https://www.webucator.com/about-us/client-list.cfm) 

https://www.webucator.com/how-to/how-generate-random-numbers-net.cfm 5/6
5/6/2020 How to Generate Random Numbers in .NET | Webucator

Company (https://www.webucator.com/about-us/company.cfm) 

Privacy Policy (https://www.webucator.com/about-us/privacy-policy.cfm) 

In The Community (https://www.webucator.com/about-us/community.cfm) 

Train for Us (https://www.webucator.com/jobs/trainers.cfm) 

Cancelation Policy (https://www.webucator.com/live-online-training/cancelation.cfm) 

Make a Payment (https://www.webucator.com/resources/make-payment.cfm) 

Certi ed Microsoft Courses (https://www.webucator.com/microsoft-training/certi ed-courses.cfm) 

Software Assurance Training (https://www.webucator.com/microsoft-training/software-assurance- 


training-vouchers.cfm)

Webucator Blog (https://www.webucator.com/blog/) 

How-To Articles (https://www.webucator.com/how-to/) 

Free Tutorials (https://www.webucator.com/tutorial/index.cfm) 

Purchase Courseware (https://www.webucator.com/courseware/index.cfm) 

   
(https://www.facebook.com/webucator)
(https://www.linkedin.com/companies/webucato
(https://twitter.com/webucator)
(mailto:sales@webucator.com)

© 2004-2020 Webucator, Inc. All Rights Reserved.

https://www.webucator.com/how-to/how-generate-random-numbers-net.cfm 6/6

You might also like