- Home /
can your Random.Range parameters match with function perameter (javascript)
So I have working code that picks a random number without having three duplicates. Here is an example below
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;
}
}
}
I have other times I want to use this but I want a different range on the random number without having a separate function for each instance I need a different range. I then thought of using parameters like so:
var previousPreviousNumber: int = 0;
var previousNumber: int = 0;
var currentNumber: int = 0;
var finalNumber: int;
function noRepeatRandom(min,max){
while (true){
currentNumber = Random.Range(min,max);
if (currentNumber != previousNumber || currentNumber != previousPreviousNumber){
finalNumber = currentNumber;
previousPreviousNumber = previousNumber;
previousNumber = currentNumber;
break;
}
}
}
noRepeatRandom(1,10);
Except I get the error:
Assets/Scripts/Menu.js(97,45): BCE0004: Ambiguous reference 'Range': UnityEngine.Random.Range(int, int), UnityEngine.Random.Range(float, float).
Its not a big deal if I can't do this because I can just make more than one function, but according to my knowledge(witch is very limited) this should work.
Answer by robertbu · Mar 20, 2014 at 12:08 AM
Try declaring your function like this:
function noRepeatRandom(min : int, max : int)
Without specifying the type of the parameters, the compiler does not know which function you want to use to process the parameters.
Answer by supernat · Mar 20, 2014 at 12:05 AM
It doesn't work because you aren't defining the type for min and max (something JS lets you do for simplicity), but Random.Range needs to know if the min/max values passed in are floats or ints. For all it knows, when you call noRepeatRandom(1,10), you might expect a float back, because 1 and 10 are ways to represent floats (unfortunately). If you always plan to use ints or floats, just cast them. I can't say 100%, because I don't use JS, but you might get away with noRepeatRandom((int)1, (int) 10). But then the code starts getting ugly. Whether the compiler lets you do that I don't know. What I mean is it may still complain that it doesn't know what the type is at the function level.
By the way, if you ever need to get a random number within a range bigger than 3, this won't work. A more general solution is to start with a list of ints from min to max, and then get a random int from 0 to sizeof list. Then get the value at that index from the list and return it. Remove the value from the list, and the list size goes down by 1 for the next frame. When the list hits 0 size, re-create it by inserting min to max numbers in it again. This guarantees random values but never the same number twice in a row (except rare boundary conditions where lets say you pick the last value in the list (4) and the list is depleted, then you recreate the list, and the next frame you have an equal chance to pull 4 again. You can't really get around that easily though, unless maybe you use some kind of modulus algorithm maybe.
declaring it worked, but the way @robertbu suggested it. And i've tested my function with a range of (1,7) and it seems to working as far as I can tell.
$$anonymous$$aybe I misinterpreted your need. I thought you wrote the function without specific types so you could call the function using floats or ints. :) With regard to repeating a number, is your intent to not have a number in the range repeat until all other numbers have been exhausted, or to only not repeat a number within the last 3 tries? If the latter, then yes your code is fine. $$anonymous$$y comment was in regard to the former. Glad you got it fixed though. :)