Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 53 / 68

Generating random numbers

This lesson covers generating random numbers with VB.NET's Random class, with the aim of building things with a random element such as dice or rock-paper-scissors.

New Random() creates a generator and rnd.Next(min, max) gives a random integer in the range you specify. Use it wherever each run should differ — games, draws.

The sample code picks a dice roll from 1 to 6 with rnd.Next(1, 7) and one of Rock, Scissors, or Paper from an array with rnd.Next(0, choices.Length). Note that the second argument of Next() is exclusive.

A common early stumble is exactly that range. Next(1, 7) returns 1 up to but not including 7 — that is, 1 to 6 — and not knowing it leaves you with the wrong range.

In professional work, random number generation is used widely — item drops in a game, generating test data, sampling for verification.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)