How To Generate A Random Number In Visual Basic
To generate a random integer in Visual Basic .NET, create a System.Random instance and call its Next method. For a number from 1 to 100 inclusive, use rng.Next(1, 101). In code that uses Rnd, initialise it with Randomize and use Int to convert the scaled.
Which licences fit your PC setup?
Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.
-

Office 2021 Pro Plus key
$57.00 AUD
-

SQL Server 2022 Standard
$399.99 AUD
-

Windows 11 Pro key
$14.99 AUD
$29.00
Choose the method for your Visual Basic project
The right starting point depends on your environment and what the number will do:
- For ordinary VB.NET tasks, such as games, sample data and simulations, use System.Random.
- For existing Rnd-based code, including classic Visual Basic and VBA, use Rnd with Randomize. The examples below use VB.NET syntax unless stated otherwise.
- For passwords, authentication tokens or other security-sensitive values, use System.Security.Cryptography.RandomNumberGenerator.
System.Random and Rnd produce pseudorandom sequences. They are useful for ordinary application behaviour, but reseeding them does not make them cryptographically secure. Random values can also repeat; a random generator does not promise a different result on every call.
Generate an integer with System.Random
This example produces a whole number from 1 to 100 in a VB.NET console application. Place the statements inside your Main procedure.
-
Create the generator before requesting any values:
Dim rng As New System.Random()
-
Call Next with the required lower bound and an exclusive upper bound:
Dim result As Integer = rng.Next(1, 101)
-
Display the result:
Console.WriteLine(result)
For another value, call rng.Next(1, 101) again using the same generator. Do not create a new instance for each draw.
The lower bound is included; the upper bound is excluded. Therefore, rng.Next(1, 100) can return 1 through 99, while rng.Next(1, 101) includes 100. A six-sided die uses rng.Next(1, 7). Values from minus 10 through 10 use rng.Next(-10, 11).
There are also shorter forms. rng.Next(100) returns an integer from 0 through 99. Calling rng.Next() without arguments returns a non-negative integer below Integer.MaxValue. Neither form needs rounding or conversion.
Reuse the generator in a Windows Forms application
If a button generates the number, declare the generator at form class level, outside the button's event handler:
Private ReadOnly rng As New System.Random()
Inside the Click handler, assign rng.Next(1, 101) to a variable and display its text in a label or text box. This keeps the generator available between clicks. Repeatedly creating generators can produce repeated sequences in some runtime environments, particularly older .NET Framework applications.
A separate reset control can clear the displayed result. Follow the guide to coding a clear button in Visual Basic if you need help wiring up that part of the form.
Generate a floating-point value
Use NextDouble when you need a fractional value rather than an integer:
Dim fraction As Double = rng.NextDouble()
The result is greater than or equal to zero and less than one. To scale it, multiply by the width of the desired range, then add the lower bound. For example:
Dim measurement As Double = 10.0 + rng.NextDouble() * 10.0
This scales the value into the interval from 10 to below 20. Floating-point arithmetic has finite precision, so do not use this approach as an exact decimal selection mechanism.
Formatting is a separate step. measurement.ToString("0.00") displays two decimal places, producing a string. It does not change the stored Double, and rounding the displayed text can show an endpoint such as 20.00.
Use Rnd and Randomize in existing code
Rnd returns a Single greater than or equal to zero and less than one. Call Randomize() once before the first draw to initialise the generator from the system timer, then request values as needed.
-
Initialise the generator:
Randomize()
-
Generate an integer from 1 through 6:
Dim roll As Integer = CInt(Int(6 * Rnd()) + 1)
Call the expression again for subsequent rolls, without reseeding before each one.
Multiplying Rnd() by six produces a value from zero to below six. Int rounds down to one of the integers zero through five; adding one gives the required die result. CInt then converts the already integral value to Integer.
Do not replace that expression with CInt(6 * Rnd() + 1). CInt rounds rather than always rounding down, so that formula can produce seven and gives uneven probabilities at the ends of the range.
For a small inclusive integer range, the equivalent expression is CInt(Int((maximum - minimum + 1) * Rnd()) + minimum). In VB.NET, System.Random.Next is usually clearer. Microsoft's Rnd documentation explains the function's range and seed behaviour.
Control repeatability with a seed
A fixed seed helps when debugging a simulation or reproducing a test case. Create the generator with an integer seed:
Dim rng As New System.Random(42)
Recreating it with the same seed and making the same sequence of method calls reproduces the sequence within the same implementation. Do not assume the output stays identical across different .NET versions. Microsoft's System.Random guidance covers this limitation and generator reuse.
Rnd has different reseeding rules. Calling Randomize with the same numeric argument alone does not restart the previous sequence. Repeating an Rnd sequence requires calling Rnd with a negative argument immediately before Randomize with a numeric argument.
For multiple threads, protect access to an ordinary shared Random instance with SyncLock. Giving each thread a generator does not guarantee unique values or non-overlapping sequences.
Use a cryptographic generator for security-sensitive values
Where your target framework supports it, generate a cryptographically strong integer from 1 through 100 with:
Dim secureNumber As Integer = System.Security.Cryptography.RandomNumberGenerator.GetInt32(1, 101)
The upper bound is exclusive here too. Microsoft's GetInt32 documentation describes its strategy for avoiding the bias introduced by simply taking random bytes modulo a range. A number from 1 to 100 is only a range example; it is too small to serve as a secure token.
Check the behaviour in a small project
Generate a batch of values and check that every result stays within the intended bounds. Expect duplicates and uneven counts in a small sample. If you need every value exactly once, shuffle a collection instead of repeatedly drawing independent numbers.
For a form-based exercise, add an option to switch between a die roll and a 1-to-100 draw using the guide to coding a checkbox in Visual Basic. The Visual Basic tutorials provide related control-handling exercises.
Start by adding rng.Next(1, 7) to a small project, display each roll, and verify that every result is between one and six.
Other editions for separate workloads
Other editions for separate workloads; prices in AUD, confirm the total on the listing.
-

Office 2024 Pro Plus key
$20.00 AUD
$39.00 -

Windows 11 Home key
$18.88 AUD
$37.00 -

Windows 10 Pro key
$12.00 AUD
$15.00
How to check this worked
- Open Settings, System, Activation and confirm the edition shown.
- Enter the key and run activation; expect a green tick within a minute.
- Reboot and re-check activation survives the restart.
FAQ
What else should you know before buying?
- What do I need before starting?
- Read the requirements and confirm the edition first; the steps assume the machine is compatible.
- How do I know the licence is genuine?
- Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
- Is this a subscription?
- Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
- What if the key fails?
- Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.
Check who prepared and reviewed this guide
- Written by
- MS.Codes editorial
- Reviewed by
- Support team
Last verified: (source review; see test status above).