# 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.

#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);
``````

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

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 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.