Hello All,
I am currently making a multiplayer fps in unity at the moment and i am stuck on something i font really seem to know why its not working for some reason this script is only choosing team blue and never team red does anybody know how to fix this
When using integers, Random.Range is _ex_clusive on the top end - you’re asking it to choose a number between 1 and 1.
The reasoning behind this: 99% of the time Random.Range is used with integers, it’s picking an item from an array. Doing it this way allows you to use someVariable = someArray[Random.Range(0, someArray.Length)];
I found that Random always neglected to pick from the top number so I used to write my random to go 1 digit higher than the top number which seemed fine but there is a very tiny chance of it actually hitting the top number as strange as that sounds because it caused my game to bug out on me and I was all like “Seriously?”
So now when I use randoms like this I tend to do this:
void Main()
{
Random rand = new Random();
int lala = rand.Next(1,3);
if(lala > 2)
{
lala = 2;
}
Console.Write(lala);
}
Is it the best solution? Probably not. But it’s worked for me. (Totally willing to hear better solutions if someone has them )
PS: I wrote that in Linqpad but the same applies either way.
Unity has its own Random class; UT implemented it themselves. It is unrelated to the Random class used in your first example. Read the docs (the second variant is the ‘int’ variant we’re discussing): Random.Range behaves as I described, and Random(1,3) will never return 3. I have been using it to select from arrays for many years, and I would have experienced many, many errors over the years if this were not the case.
Yet the example with System.Random will return a 3 eventually but it’s a rare case. Perhaps Unity’s setup is a bit more reliable to use then. Thank you for the educational lesson.
So if you use Random.Range you are guaranteed to never hit the top number. If you use System.Random the top number will be hit at a very rare case but this would cause you to end up with the maximum number being hit extra times.
So Random.Range is a better way to go than System.Random and it supports floats.
public void DoRandomThing()
{
int lala = UnityEngine.Random.Range(1, 3);
print(lala);
}
I still don’t know how much I’d trust that but go for it. If it ever does land on that 3 for whatever reason then you’d end up with an exception error though.
No trust is required; this is incredibly easy to test:
using UnityEngine;
using System.Collections;
public class RandomTester : MonoBehaviour {
public int rangeTop = 3;
// Use this for initialization
void Start () {
int[] randomCounts = new int[rangeTop+1];
for (int j=0;j<2000;j++) {
int index = Random.Range (0, rangeTop);
randomCounts[index]++;
}
string result = "Results:";
for (int r=0;r<randomCounts.Length;r++) {
result += "\nIndex "+r+" was chosen "+randomCounts[r]+" times.";
}
Debug.Log (result);
}
// Update is called once per frame
void Update () {
}
}
Result:
Results:
Index 0 was chosen 670 times.
Index 1 was chosen 645 times.
Index 2 was chosen 685 times.
Index 3 was chosen 0 times.
I did the same test with System.Random and got the same result:
System.Randomrand = newSystem.Random();
for (intj=0;j<2000;j++) {
intindex = rand.Next(0, rangeTop);
randomCounts[index]++;
}
~~~~~~
Results:
Index 0 was chosen 670 times.
Index 1 was chosen 645 times.
Index 2 was chosen 685 times.
Index 3 was chosen 0 times.
I ran it with much higher counts for random attempts and index 3 is never chosen no matter how many times I run it.
Can you demonstrate a case where this does not work?
You are actually right as I went back and looked at the test I made in Linqpad and typod a number lol.
void Main()
{
Random rand = new Random();
int l = rand.Next(0,3);
for(int i = 0; i < 1000000; i++)
{
l = rand.Next(0,3);
if(l == 3)
{
Console.Write("Hit 3");
break;
}
}
}
This does not hit 3 at all no matter how many times I run it. I’m now unsure of where my paranoia and need to write that if statement making sure it doesn’t hit the top number came from >_>
Guess I owe you a thanks for setting me straight with this. It’ll be a relief next time I’m coding random stuff to know that going 1 number over the actual max value is safe without an if check.