How to generate a Random Float Variable

Hello I need to Generate a Random Float Variable not Int here is what I got so far.

var number = Random.value (0,3);

You’re on the right track, but there are a few changes needed to make this work the way you seem to be hoping for.

First, your intent would be better represented using Random.Range(). With that in mind, there are two variations of that function. One returns an integer if both arguments are integers and one returns a float if both arguments are floating point numbers.

For convenience and computational simplicity, a number represented without a decimal place will be treated as an integer, while a number with a decimal place will be treated as a float or double (depending on language, in the case of Unity).

All in all, this means that the change to make it work for you will be very simple:

// JS
var number: float = Random.Range(0.0, 3.0);

// C#
float number = Random.Range(0.0f, 3.0f);

By representing both function arguments as floating point values, the overloaded function to return a float will be selected between the two. As a result, you will see any decimal value between 0 and 3 as your return value rather than only seeing 0, 1, and 2.

Edit: Added an explicit type to the Unityscript variant.


float number = Random.Range (0f, 3f);

The docs are your friend:

Close, but value is a property of the Random class not a method, so you don’t need the brackets.

var number = Random.value;

That will give a number between 0.0 and 1.0, which you can then multiply by 3 (or any other value).

Hi , Try using Random.Range . It takes to arguments , A min and max . In your case , It will be something like this : Random.Range(0f, 3.0f)

Hi, I tried using this but then I just get an error telling me to do it in Start() or Awake() instead. I really can’t find a solution to this. Here’s my code:`

public float health = 100f;
public Transform firePoint;
public GameObject bulletPrefab;

[SerializeField] float number = Random.Range(0.4f, 1.4f);

void Start()


// Update is called once per frame
void Update()
    if(health <= 0f)

private void OnTriggerEnter2D(Collider2D collision)
    if (collision.gameObject.CompareTag("Range") || collision.gameObject.CompareTag("Bullet"))
        health -= 2f;

    if (collision.gameObject.CompareTag("Melee"))
        health -= 10f;

void Shoot()

IEnumerator ShootRepeat()
    Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    yield return new WaitForSeconds(number);

Please help with this.