Tutorials Icon

Tutorials > Create a Random Number Generator

Create a Random Number Generator

By Stephen Armstrong // December 18, 2019


Learn how to add a random number generator to your game.

This tutorial will help you create a random number generator.

Random number generators are an easy way to add greater variety to your game.

As this generator uses pure C# code, it can be used in any game engine or framework that uses C# (such as MonoGame and Unity).

Create a RandomNumber class

Right click on your project in Solution Explorer and select Add > New Item…

A screenshot of Visual Studio

In the Add New Item window, select Class and enter a Name. In this example we call it RandomNumber.cs. Click Add when finished.

A screenshot of Visual Studio

RandomNumber.cs should automatically open, and you’ll see the following:

A screenshot of Visual Studio.

Feel free to delete this default code, and replace it with the following:

using System;

class RandomNumber
{
    static Random rnd = new Random();
    
    public static int RandomInt(int min, int max)
    {
        // Switch around numbers if min is higher than max. The game will crash if min is higher than max.
        if (max< min)
        {
        return rnd.Next(max, min);
        }
        
        return rnd.Next(min, max);
    }
}

As a public and static method, RandomNumber.RandomInt() can be directly called by any part of your game.

Using the RandomNumber class

To generate a random number between a set minimum and maximum value, all you have to do is call RandomNumber.RandomInt() with a minimum and maximum value.

Open Game1.cs and add the following code to the Update() cycle:

// Set the minimum and maximum values
int minimum = 0;
int maximum = 1000;

// Get the random number
int randomNumber = RandomNumber.RandomInt(minimum, maximum);

System.Console.WriteLine("A random number between {0} and {1}: {2}", minimum, maximum, randomNumber);

If you run the game now and check the Output, you will see a constant generation of random numbers between 0 and 1000.

A screenshot of random numbers being generated.

Get random values from arrays

Sometimes you may find it better (or necessary) to choose a random number from pre-selected values.

The following code is an example of building a sentence using string arrays:

// An array of names
string [] names = new string [] {"James", "John", "David", "Murray", "Ibrahim", "Dae-Hyun", "Esteban", "Connor", "Luke", "Cain", "Francois", "Herman", "Joseph", "Ivan", "Lloyd", "Michael", "Diego" };

// An array of greetings
string [] greetings= new string [] { "Hello", "Goodbye", "Good morning", "Good afternoon", "Good night", "Please", "Thank you", "Sorry", "Excuse me" };

// Get a random name
string randomName = names[RandomNumber.RandomInt(0, names.Length)];

// Get a random greeting
string randomGreeting = greetings[RandomNumber.RandomInt(0, greetings.Length)];

// Output a sentence based on a randomly-selected name and greeting
System.Console.WriteLine("{0} says \\"{1}\\"", randomName, randomGreeting);

If you run the game now and check the Output, you will see a constant generation of random sentences.

A screenshot of random sentences being generated.

Please note

One final thing to note, is that while this random number generator is good enough for use in a video game, the resulting number is not a “real” random number (it is called a pseudo-random number).

You should not use this random number generator if you need a completely random number (for generating passwords or other security/cryptography purposes).

< Go back

Return to top of page

Article Icon

Welcome to Industrian.net!

On this website you'll find more information about our games, and also some tutorials for you to start making games of your own! You can also follow us on various social platforms!