- Home /
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;
}
You always want to generate a random number, right? So it shouldn't be inside an if clause like in your version. You want to generate a random number first, then test if it's the same as the previous one (and keep regenerating till it isn't) - this is what Griffo's answer shows.
The other thing about your code is that you don't show us what b
is. It must be referred to elsewhere in your code. Without that it's hard to tell what your code is even trying to do.
Answer by Griffo · May 11, 2016 at 09:45 AM
I use ..
private int a;
private int oldNumber;
void randomController()
{
a = Random.Range (0, 4);
oldNumber = a;
if(a == oldNumber)
{
randomController();
}
}
You're assigning ins$$anonymous$$d of comparing on line 8
Probably better off using a loop. Recursion seems awfully expensive for what you're trying to do. And in the (admittedly extremely unlikely) chance you get the same number often enough you could overflow the stack. Just doesn't seem like a good idea to leave that up to chance.
I've just noticed that this isn't going to work as written. The test will always succeed because you set oldnumber to be equal to a immediately before testing if it is equal to a. Easy enough to fix, but I agree with Dave, it's simpler and better with a loop...
int previous = a;
while (a == previous)
{
a = Random.Range(0,4);
}
Answer by tanoshimi · May 11, 2016 at 06:22 AM
Random.value will do that just fine. http://docs.unity3d.com/ScriptReference/Random-value.html
Answer by YoungDeveloper · May 11, 2016 at 09:49 AM
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.
Answer by meat5000 · May 11, 2016 at 12:02 PM
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.
Answer by kaphuochung24h · Mar 24, 2017 at 10:53 PM
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[i] = Random.Range(1, 7);
if (i > 0)
{
previous = 0;
check = 0;
do
{
if(previous >= i){
previous=0;
check = 0;
}
if (pos[i] == pos[previous])
{
pos[i] = Random.Range(1, 7);
}
else
{
check++;
}
previous++;
} while(check == i);
}
}