Adding randomness to your game through code using Unity 3d

There are several different ways to add randomness through code. I'm going to mostly talk about the built in random number generators and examples of where to use them. But first I will touch on why it is also important to understand the statistics of a random number generator.
*I'm going to reuse the word number when describing randomness, however what the number represents can be anything. Such as a random color(RGBA hex code) or choosing a random texture from an array.*

Randomness can be used in several ways. Two of the most common are below. 
Example 1. Where any number is used regardless of the previous.
Example: When using 1-10 at ten iterations any number is used. So there can be three 1's, four 2's, zero 3's,... This is achieved simply using Unity's Random.Range() or System.Random.Next().

The statistics of example 1:
Each number may or may not be used equally and every number within the constraints may or may not be used.
Example output at ten iterations: 1s:1, 2s:0, 3s:2, 4s:3, 5s:0, 6s:0, 7s:1, 8s:1, 9s:2, 10s:0.

Example 2. Where each number is used once or before a re-occurrence of that number is used again.
Example: When using 1-10 at ten iterations each number occurs. So there is only one 1, one 2, one 3,... This is achieved through extra code in Unity and one example is shown below.

The statistics of example 2:
Each number will be used equally and every number within the constraints will be used.
Example output at ten iterations: 1s:1, 2s:1, 3s:1, 4s:1, 5s:1, 6s:1, 7s:1, 8s:1, 9s:1, 10s:1.

Example 2 has an equal opportunity to display a number within that data set, the random part is when each number appears within those iterations. Example 1 is much more, well, random. Both of these have positives and negatives and when/how you use them is up to you! Below are some examples of where both can be helpful.

Real use case:
Below is a slightly modified code excerpt from my smash hit (not really) Galaxy Shooter where I use both examples.
// public so gameobjects can be added from prefabs
public List<GameObject> enemies = new List<GameObject>();// the list should be copied or saved prior if you dont want items removed
void DisplayEnemy(Vector3 position)
    // pick an enemy from the list randomly // At the end on this method 'num' is removed from the list. This is Example 2.
    int num = Random.Range(0, enemies.Count);
    GameObject theEnemy = (GameObject)Instantiate(enemies[num], transform.position, transform.rotation);
    // place the enemy (the Vector3 argument 'position' would be best to use here so enemies can be placed where it makes sense in your game)
    // 'Random.Range(35, 85)' is Example 1.
    theEnemy.transform.position = new Vector3((float)Random.Range(35, 85), (float)Random.Range(-25, 30), transform.position.y);
    // remove that enemy from list
    // now the next time you call this method there will be one less and all the enemies can be displayed randomly
// Bonus code below!
// if you want to show all your enemies
void DisplayEnemies()
    for (int i = 0; i <= enemies.Count + 1; i++)
        DisplayEnemy(new Vector3((float)Random.Range(35, 85), (float)Random.Range(-25, 30), transform.position.y));

Weighted randomness is the next frontier and a non-coding way to do this using the code above would be to add several of the same prefab enemys to the list so they show up more than others. Coding weighted randomness is a bit more complex so I'll cover it in detail in the future. Thanks for reading!

Super helpful links!
Unity docs Adding Random to your Game
Unity docs Random class
Dot Net's Random class


  1. This code is really helpful for those who are thinking to play Unity 3D games. Hire the best best unity programmer who will guide you for remote unity developer and different types of unity games.


Post a Comment

Popular posts from this blog

Dungeon Traveler

How to create slowly revealed text in a Unity 3d game

Fade in and fade out Script for Unity 3d