Non Repeating Random Numbers between a range.

Updated on June 9, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on June 8, 2018

I have some mudholes GameObject and mole is coming out of them randomly every  second but the problem is Random.Range(min,max)repeats the mudhole number because of same number generating repeatedly sometimes. How do I generate a random Number between the min and max range without repeating?

 

Thanks in Advance for the help. I hope someone can some up with a solution.

 

public List mudHoles = new List();
public GameObject Moles;
 float spawnTimer = 1f;
void Start()
{
foreach (GameObject mudhole in GameObject.FindGameObjectsWithTag("GroundHole")) 
 {
 mudHoles.Add (mudhole);
}
}
 
void FixedUpdate()
 {
if (spawnTimer >= 0) 
 {
 spawnTimer -= Time.deltaTime;
}
if (spawnTimer <= 0) 
 {
 spawnModi ();
 spawnTimer = 1f;
 }
 }
 
void spawnMole()
 {
Vector3 _cposition;
 GameObject _mole = Instantiate(Moles);
_cposition = mudHoles [Random.Range(0,mudHoles.Count)].transform.position;
 _mole.transform.position = new Vector3(_cposition.x, _cposition.y, _cposition.z);
 }
 
  • Liked by
Reply
1 on June 8, 2018

If you don’t want them to repeat back to back, but are fine with them repeating after a different one has happened, you could store the random number, and if it comes up again before you change it, just keep trying for a new random number.

int lastRandomNumber = 0;
void spawnMole()
 {
    Vector3 _cposition;
    GameObject _mole = Instantiate(Moles);
    int newRandomNumber = -1;
    while (newRandomNumber == lastRandomNumber)
    {
        newRandomNumber = Random.Range(0,mudHoles.Count);
    }
    lastRandomRumber = newRandomNumber;
    _cposition = mudHoles [newRandomNumber].transform.position;
    _mole.transform.position = new Vector3(_cposition.x, _cposition.y, _cposition.z);
 }
on June 9, 2018

Thank you so much. I was trying to solve it using do while but while did it perfectly.

I am so grateful. 

Show more replies
  • Liked by
Reply
Cancel
0 on June 8, 2018

If you don’t want duplicates, try something like this:

List<int> usedRandomNums = new List<int>();
void spawnMole()
{ 
    int rndNumber = Random.Range(0, mudHoles.Count);
    // Run a total of n * n times.
    for(int n = 0; n < usedRandomNums.Count; n++)
    { 
        // Check if unique number generated.
        if (rndNumber == usedRandomNums[n])           
        {
            rndNumber = Random.Range(0, mudHoles.Count);
            n = 0;
        }
        else
        {
            usedRandomNums.Add(rndNumber);
            j += usedRandomNums.Count * 2;
        }
    }
    // Spawn hole.
    Vector3 _cposition;
    GameObject _mole = Instantiate(Moles);
    _cposition = mudHoles[rndNumber].transform.position;
    _mole.transform.position = new Vector3(_cposition.x, _cposition.y, _cposition.z);
}

I don’t see any issues with this code, as so long as you have a decent range. If it isn’t, then just modify the code. Although, I wouldn’t use a while loop as they tend to break Unity.

BTW I haven’t tested this code, so expect bugs.

  • Liked by
Reply
Cancel