Random number different from previous generated.

Hi! I’m really all new to this programming stuff. I’m having a problem with Unity’s random number generator as it has a tendency (well at least in my case) to consecutively repeat the same number a lot.

I’ve tried figuring how to code it myself to no avail. When I use a while loop the engine tends to crash which makes me believe I’ve written it wrong.

I just want a c# script that prevents it from repeating the same number that was generated previously.

I’ve tried making this script but all I keep getting is 0. Help please?

void randomController ()
{
a = Random.Range (0, 4);

	if (a == b)
		a = Random.Range (0, 4);
	else
		a = b;

				
}

I use …

private int a;
private int oldNumber;

void randomController()
{
     a = Random.Range (0, 4);
     oldNumber = a;
     if(a == oldNumber)
     {
          randomController();
     }
}

Random.value will do that just fine.

Don’t expect equally filled pattern with Random, because that’s what it is, it’s random. Argument i’m getting 0 a lot is not really valid, because you know it can be whatever it can be in those scopes.

What exactly you mean a lot?

void Start(){
    for(int i = 0; i < 1000; i++){
        Debug.Log(Random.Range(0,4).ToString());
    }
}

If youll get thousand zeros in debug ill eat my hat.

If you are truly getting repeat results when you re-run the same tests with ‘Random’, change the seed.

http://docs.unity3d.com/ScriptReference/Random-seed.html

Also int overload of Random.Range is Maximally exclusive. In actual english this means that the upper number is never reached. 4 will never occur.

Also, the idea of removing the object/number when it is selected is known as a Shuffle Bag.

Maybe this is the old answer for the topic owner, but i hope it can help someone who can’t get the right answer.

for (int i = 0; i < pos.Length; i++)
        {
            pos *= Random.Range(1, 7);*

if (i > 0)
{
previous = 0;
check = 0;
do
{
if(previous >= i){
previous=0;
check = 0;
}
if (pos == pos[previous])
{
pos = Random.Range(1, 7);
}
else
{
check++;
}
previous++;

} while(check == i);
}
}

Maybe this would be helpful for someone. I was trying to spawn enemy planes in Augmented Reality. I wanted them to be randomly spawned but at a distance. For example, if one plane is spawned at 0,0,0. I wanted the next plane to be spawned at 0+20 or 0-20 on the X-Axis. Like, the new plane should spawn at 20 or more points from the present spawned point in positive and negative direction. Going through the forum icame across a code and modified it and then came to this:

 void RandonDistanceAxis()
 {
     distanceX = Random.Range (-70, 70);
     while (distanceX <= tempx + 20 && distanceX >= tempx - 20 )
     {
         distanceX = Random.Range (-70, 70);
     }
     tempx = distanceX;
     distanceY = Random.Range (-10,10);
     distanceZ = Random.Range (60,90);
 }

While declaring the tempx variable, give it a value of something out of the Range. I used 71. And it worked perfectly fine. Hope this helps. Cheers.