Generate a random number different to the last
Hi there,
I need to generate a random number in unity that will be different to the last generated random number.
I have the following code and this is how you would do it in something like flash.
function getUniqueRandomSectionNumber(){
var tempSectionNum : int = Random.Range(0, totalSections);
if(tempSectionNum == lastCreatedSectionNum){
getUniqueRandomSectionNumber();
} else {
lastCreatedSectionName = tempSectionNum;
return tempSectionNum;
}
}
However in Unity I don't seem to be able to run the function from inside itself. In order to try again on the random number.
Hope this makes sense and thanks for any help
Will
just add a random number between one and the number. modulo by the size. it's that simple.
Answer by Random username · Oct 22, 2012 at 03:12 PM
You could have the code in a while loop that only ends when the last random number is not equal to the current one. e.g:
function getUniqueRandomSectionNumber(){
var hasFoundUniqueRandomSelection : bool;
var tempSectionNum : int;
while (!hasFoundUniqueRandomSelection) {
tempSectionNum = Random.Range(0, totalSections);
if(tempSectionNum != lastCreatedSectionNum){
hasFoundUniqueRandomSelection = true;
lastCreatedSectionName = tempSectionNum;
}
}
return tempSectionNum;
}
Apologies if I've made any mistakes, not accustomed to working with javascript.
Thanks for that I figured out that my code above does actually work too I just needed to declare the type as int.
function getUniqueRandomSectionNumber() : int { }
The OPs code is using recursion, which is the same thing as that loop.
Real error is needing to add return to the true part of the if: `RETURN getUniqueSecNum();`.
You'd think "but the else has a return." But say you roll the last number twice in a row. You've got a chain of three functions. the last one rolls a fresh num and returns to the previous function, which called it. That one also needs to return the number up the line. And if that doesn't make sense, just use the loop :-)
you would never, ever do it that way
But there is an unbelievably easier way to write that!
result = random
while ( result != previous ) result = random
that's all it is.
Goodness! You young people, really :-)
Answer by srikaran_p · Sep 10, 2016 at 12:17 PM
Sir/Ma'am, even I had the same problem before but I fixed it with an if-condition.
currentNumber = Random.Range (0, markers.Length);
if (lastNumber != currentNumber)
{
lastNumber = currentNumber;
} else
{
currentNumber = Random.Range (0, markers.Length);
lastNumber = currentNumber;
}
this does not work, because you could AGAIN draw the same random number :)
the correct answer is in my short comment under the question. it is a very famous algorithm in computer science - very simple.
cheers
It worked really well for me after testing the same function for about more than 100 times. And, there was only 7 numbers in my array so it could have been repeated but it didn't, so that's why I posted this answer. Well, I'm a very young programmer and I looked for an answer and I also looked at your comment but it wasn't what I wanted and then , I coded this myself. It worked really well so I posted this answer even though it is 4 years old. But anyways, I can't say wrong to an experienced programmer.
Answer by TinoCle · Nov 13, 2016 at 10:38 PM
int rand = Random.Range(0,totalSections);
while(rand==RandyAnterior){
rand = Random.Range(0,totalSections);
}
RandyAnterior = rand;
Answer by ayhaab-pasha · Oct 24, 2018 at 07:23 AM
I know its 2018 but people would still be stumbling upon this question. So, 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 i came 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.