Random.Range not working?

Hey, beginner here, so I’ve tried using Random = UnityEngine.Random and UnityEngine.Random.Range, but neither seem to be working. I just want to print 0 or 1 depending on whatever random number is chosen from the array. All it does is return 0 over and over. Help??

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class HostScript : MonoBehaviour
{
  
    void Update()
    {
    
     int hostStates = (UnityEngine.Random.Range(0, 1));

      if(hostStates == 0)
      {
        
          print("0");

      }

      if(hostStates == 1)
      {
        
          print("1");
      }

}

I think you are misunderstanding something here.

using System.Collections;

This tells the compiler, that you intend to use the System.Collections namespace.

using Random = UnityEngine.Random;

This basically defines an alias that you want to use “Random” as “UnityEngine.Random”. This is only really useful when you have two different “Random” namespaces (like C#s own Random and Unity Random) and want to define what you mean when you just write “Random”. So not really what you are looking to do here.

It’s always worth to take a look at the documentation:

There are also examples included. In this case, you simply write Random.Range(0,1). Be aware tho, that for integer values, the max value is exclusive, so this would only ever return 0. What you want for a result of possibly values of {0,1} is Random.Range(0,2).

4 Likes

UnityEngine.Random.Range is simpler to understand than System.Random.Next, and probably the one you want to be using in this case. It is also included in the UnityEngine namespace so you do not need to import anything extra.

As you want to be generating integers, you would pass in integers to the method, but you must remember that the minimum value is inclusive (i.e. it has a chance to be picked), however the maximum value is exclusive (i.e. it will not be picked, but the integer below it will). It is this way because the Unity team know that people are going to be using it to generate random index values often, and as you already know indexing in C# (and most other languages excluding - most notably - Lua), starts at 0.

In this case, what you want to be doing is this:

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

public class HostScript : MonoBehaviour
{
     void Update()
     {
          int randomInt = Random.Range(0, 2);
          Debug.Log(randomInt);
     }
}

Remember the max value is exclusive, so I’ve used 2, because now this will either output 0 or 1.

See how I’ve sent the random number straight into the print method? This will allow me to make my code much more readable, faster, and easier to write once I have multiple possible numbers (imagine if I waned my random number to be from 0 to 10,000! I would not want an if statement for every possibility).

If you only wanted your number temporarily, and don’t need it anywhere else in your Update() loop, you could even do Debug.Log(Random.Range(0, 2));.

Hope this clears things up.

BTW, see the link for what you did in line 4:
https://stackoverflow.com/questions/505262/c-sharp-namespace-alias-whats-the-point

3 Likes

Thanks for the explanation, and for the link. It works now!

Thanks man it work for me

Please use the like button to show appreciation instead of necro postings.
Thread locked

4 Likes