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);

5 Answers

5

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.

try

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()
{
 
StartCoroutine(ShootRepeat());

}

// Update is called once per frame
void Update()
{
    if(health <= 0f)
    {
        Destroy(gameObject);
    }
   
}

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.
Thanks

This answer: - Bumped a 6 years old question that has been answered already - is not an answer to the question that was asked - tries to hijack this question by asking a completely seperate question in an answer When you have a question, please ask your own question and do not post answers to unrelated questions. In short: you can not use any Unity API calls like Random.Range inside a field initializer.

Solution is to wait. My last game took 3 days to be searchable by unity dashboard