- Home /
Random number that doesn't repeat. (javascript)
I'm not sure if its my logic that doesn't make sense or some syntax error, but it doesn't work. What I am trying to do is have a function that creates a random number every time you call the function. The only catch is three of the same numbers can't be created in a row. For example: 1,1,2 would work. 1,2,1,2,1,2,1,2 would also work. But 111 would not work. So if the number matches the previous two, it should choose another random number. Here is the errors I get:
Assets/NewBehaviourScript.js(13,10): BCW0023: WARNING: This method could return default value implicitly.
Assets/NewBehaviourScript.js(35,33): BCW0015: WARNING: Unreachable code detected.
Here is my current code:
#pragma strict
var firstNumber: int;
var secondNumber: int;
var thirdNumber: int;
var firstTry: boolean = false;
var secondTry: boolean = false;
var thirdTry: boolean = false;
function noRepeatRandom(){
if (firstTry == false){
firstNumber = Random.Range(1,4);
firstTry = true;
return firstNumber;
}
else if (firstTry == true && secondTry == false){
secondNumber = Random.Range(1,4);
if (secondNumber != firstNumber){
firstTry = false;
}
else{
secondTry = true;
}
return secondNumber;
}
else if (secondTry == true){
while (true){
thirdNumber = Random.Range(1,4);
if (thirdNumber != secondNumber){
secondTry = false;
return thirdNumber;
break;
}
}
}
}
Debug.Log(noRepeatRandom());
I've used the return command in python but never in JavaScript, so those might be the problem.
Added a code below. A little simpler than what you had, but it should do the job. There was no need for a return in the code, since the variable were being set within the function.
Answer by byurocks23 · Mar 19, 2014 at 11:26 PM
I think I have found my solution
var previousPreviousNumber: int = 0;
var previousNumber: int = 0;
var currentNumber: int = 0;
var finalNumber: int;
function noRepeatRandom(){
while (true){
currentNumber = Random.Range(1,4);
if (currentNumber != previousNumber || currentNumber != previousPreviousNumber){
finalNumber = currentNumber;
previousPreviousNumber = previousNumber;
previousNumber = currentNumber;
break;
}
}
Debug.Log("finalNumber " + finalNumber);
}
InvokeRepeating ("noRepeatRandom",2,1);
I've been running this for some time and I haven't encountered it running into three in a row.
Answer by Dblfstr · Mar 19, 2014 at 08:37 PM
#pragma strict
var firstNumber: int;
var secondNumber: int;
var thirdNumber: int;
var differentNumber: boolean = false;
function noRepeatRandom(){
firstNumber = Random.Range(1,4);
secondNumber = Random.range(1,4);
thirdNumber = Random.range(1,4);
while(!differentNumber)
{
if(secondNumber == thirdNumber){
Debug.Log("Second and third number are the same")
thirdNumber = Random.range(1,4);
}
else{
Debug.Log("Second and third number are different")
differentNumber = true;
}
}
}
Debug.Log("First Number :" +firstNumber);
Debug.Log("Second Number :" +secondNumber);
Debug.Log("Third Number :" +thirdNumber);
This works but the problem is that it creates three variables, each with different numbers. What I want is that each time the function is called, it creates one random number that cannot be equal to the two consecutive numbers before it.
Yep, that is what this does,and is how I thought you wanted it to work. Sorry bout that.
Answer by TykoX64 · Mar 19, 2014 at 08:39 PM
If you're using the function to set all three variables in 1 go as it looks like you shouldn't need to call return at all. just get the numbers, exit your while loop after the third number with break and be on your merry way :)
The problem your having here
Assets/NewBehaviourScript.js(35,33): BCW0015: WARNING: Unreachable code detected.
is that it gets to the first return and exits the function and will always exit the function there. return tells the function that you've done what you need to and to pass the variable after it to whatever called the function.
function GetValue() : int
{
if (weWantValueX)
return X;
else
return Y;
}
This will test if weWantValueX and if so gives back X and ends the function, otherwise it continues and returns Y. if you have any other questions about returns just let me know.
I got my code error free and ins$$anonymous$$d of using returns, I set the chosen value to a variable. The only problem is it seems it works for the first three numbers, but after that it sometimes has three in a row and sometimes four in a row. (It does seem less common though)
what you want to do is set up an initializer function to create your first three numbers, then call a newNumber function to create just 1 new number.
EDIT: Apparently do while loops aren't supported in javascript... so here's the change.
var firstNumber : int;
var secondNumber : int;
var thirdNumber : int;
function SetUp()
{
firstNumber = Random.Range(1,4);
secondNumber = firstNumber;
while (secondNumber == firstNumber)
{
secondNumber = Random.Range(1,4);
};
thirdNumber = secondNumber;
while (thirdNumber == secondNumber)
{
thirdNumber = Random.Range(1,4);
};
}
function GetNext ()
{
firstNumber = secondNumber;
secondNumber = thirdNumber;
while (thirdNumber == secondNumber)
{
thirdNumber = Random.Range(1,4);
};
}