Picking random floats, but not within a range

So what I want to do is pick a random array of floats, but throw out any values between -0.25 and 0.25. Effectively eliminating 0 and values close to 0.

Here is the test code, that doesn’t work, although I think it should:

var values:float[];
private var num:float;

function Start () {
    for(var i:int = 0; i<values.length; i++){
        values[i] = PickRandomFloat();
    }
}

function PickRandomFloat(){
    num = 0.0;
    num = Mathf.Lerp(-1.0, 1.0, Random.value);
    if(num < 0.25  num > -0.25){
        print("Picked " + num + ", picking again.");
        PickAgain();
    }
    else{
        print("Picked " + num + ",returning value");
        return num;
    }
}

function PickAgain(){
    PickRandomFloat();
}

I have attached a package that demonstrates the issues I am seeing. Just import and hit play. In theory, this should throw out any values between -0.25 and 0.25 and pick a new number again, and if you look at the console output, it says it is throwing out invalid values, and picking again.

However, if you look in the inspector, there are 0’s in the array.

I am completely confused. Any pointers are appreciated.

465674–16324–$BadRandomNumbers.unitypackage (3.86 KB)

It seems like having a global variable, but then the function returns that instead of using a local variable, probably won’t work. In any case it’s not necessary to use recursion:

function PickRandomFloat () {
	return Random.Range(.25, 1.0) * (Random.value < .5? 1 : -1);
}

–Eric

Your my hero Eric.

Now to re-read up on those pesky ternary operators…

So for everyone who needs to understand what that bit of code from Eric is doing…

return Random.Range(.25, 1.0) * (Random.value < .5? 1 : -1);

Says, Return to me a random value between .25 and 1.0, but before you give it back to me, pick a random value between 0.0 and 1.0, and if that value is less than .5, multiply the first picked value by 1, and if the value is greater than .5, multiply it by -1.