0

I'm looking for the fastest way to pick a random object that has a certain condition (from an array).

In the example below I have a multidimensional array, 50 * 50 that contains objects. I want to pick a random object from that array but that object needs to have a size larger than 100.

while (object.size <= 100)
{
    attempts++;
    object = grid_array[Math.round(Math.random() * 49)][Math.round(Math.random() * 49)];
}

Currently I have tested this and in some instances it takes over 300+ attempts. Is there a more elegant way to do this?

Thanks,

2
  • If you want to pick a random object that has a size larger than 100 you need to change the while condition to: while( object.size <= 100 ). The time that it takes depends of how much objects have a size larger than 100 in your Array. Commented Jan 4, 2016 at 15:57
  • Sorry, that was only for demonstration purposes, I have amended my post, thanks. Commented Jan 4, 2016 at 16:07

2 Answers 2

2

What I would do is first filter the source array to extract only valid candidates, then return a random one (if there are any).

For example:

function getRandomObject(grid_array:Array, minSize:Number):Object {
    var filtered:Array = [];
    for(var i:int = 0; i < grid_array.length; i++){
        var inner:Array = grid_array[i];
        for(var ii:int = 0; ii < inner.length; ii++){
            var object:Object = inner[ii];
            if(object.size >= minSize){
                filtered.push(object);
            }
        }
    }
    return filtered.length ? filtered[int(Math.random() * filtered.length)] : null;
}

// example:
var object:Object = getRandomObject(grid_array, 100);
if(object){
    // do stuff with `object`
}
Sign up to request clarification or add additional context in comments.

Comments

1

I asked if you need the indexes because you could do this with RegExps and the JSON Class (Flash Player 11). With this example I stored the indexes of the objects:

Create random multidimensional Array to test the function

//---I stored a variable size and the indexes inside the Object
//---Size variable will be numbers between 0 and 500
var array:Array = [];

var i;
var j;
var size:uint = 50;
var obj:Object;

for(i = 0; i < size; i++){

    array[i] = [];

    for(j = 0; j < size; j++){

        obj = new Object();
        obj.size = Math.floor(Math.random() * 500);
        obj.files = i;
        obj.columns = j;

        array[i][j] = obj;      

    }

}

Method to get random Object with size property bigger than 100

//---I'll use to search the object a JSON string
var str:String = JSON.stringify(array);

//---Function to get the random Object
function getRandom():Object{

    //---RegExp to search object with size between 100 and 500
    var reg:RegExp = /\{[^\}]*"size":(?:10[1-9]|1[1-9]\d|[2-5]\d\d)[^\}]*\}/g;

    //---Get all matches
    var matches:Array = str.match(reg);

    //---Return a random match converted to object
    //---If no match founded the return will be null
    return matches ? JSON.parse( matches[Math.floor(Math.random() * matches.length)] ) : null;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.