pageTitleCS

Generating random numbers is an excellent way to add variation to your game and keep users guessing as to the structure of your game. This could be by placing objects at random spawn points or giving AI random move speeds. In this article we will look at two different methods to generate random numbers and assign them to variables. You can use the unity example below to test the methods we will be looking at.

 

Random.value
Random.value generates a random float between 0 and 1 (eg. 0.4). In the example script below we will generate a random float at runtime using the Start function and assign it to a variable.

JS

#pragma strict

// Create float variable to hold the generated number
var randomNumber : float;

function Start () {

// Set the variable equal to the float generated by Random.value 
randomNumber = Random.value;

// display the number in the console window
Debug.Log(randomNumber);

}

C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class randomGenerator : MonoBehaviour {

// Create float variable to hold the generated number
float randomNumber;

// Use this for initialization
void Start () {

// Set the variable equal to the float generated by Random.value
randomNumber = Random.value;
// display the number in the console window
Debug.Log(randomNumber);

}

}

To use this method to gain a number other than between 0 and 1, you can times the Random.value by the upper value you require. (e.g. Random.value * 10) this will give you a random generated number between 0 and 10.

 

Random.Range
Random.Range generates a random int between two numbers or Range. In the example script below we will generate a random int at runtime between 10 and 20 using the Start function and assign it to a variable.

JS

#pragma strict

// Create int variable to hold the generated number
var randomNumber : int;

function Start () {

// Set the variable equal to the int generated by Random.Range
// ** NOTE: we have to set the top range value to one above the required range.
randomNumber = Random.Range(10,21);

// display the number in the console window
Debug.Log(randomNumber);

}

C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class randomGenerator : MonoBehaviour {

// Create int variable to hold the generated number
int randomNumber;

// Use this for initialization
void Start () {

// Set the variable equal to the int generated by Random.Range
// ** NOTE: we have to set the top range value to one above the required range.
randomNumber = Random.Range(10,21);
// display the number in the console window
Debug.Log(randomNumber);

}

}

This method works well when coupled with a switch statement to random genertate different objects and can be useful when building procedural environments.

Casino Bet 365 is best casino in the world.

What's Next

Next up for Unity Snippets is a look at game development articles from across the web...

Free Templates - bigtheme.net
Popular Art Betting make bookies articles.