开发者

How to pick random element in an array AVOIDING (EXCEPT if it's) a certain value?

For example, when users are connecting to this application they are given a userid var, then it's inserted into the array. So, i'm using

chosenUser = usersOnlineArray[Math.round((Math.random()*usersOnlineArray.length))];

to pick another random user. But i need to mak开发者_StackOverflowe sure it doesn't pick the userID they personally were assigned so they don't end up getting themselves.

how would i use that code but tell it to "remember, make sure you don't pick userid"

maybe I could do something like

chosenUser = usersOnlineArray[Math.round((Math.random()*usersOnlineArray.length))];

    if (chosenUser = userid)
    {
        chosenUser = usersOnlineArray[Math.round((Math.random()*usersOnlineArray.length))];
    } else
    {
    //next part of mycode
    }

Maybe that should be a while (chosenUser = userid) incase it gets it twice...

But, i'm thinking i could skip all that if there is a efficent way just to make sure it doesn't pick it in the first place.

Sorry, i'm not quite sure how I should have phrased the question or if there is a term i'm unaware of to refer to what i'm trying to do.


using Math.round() can lead to returning "undefined" since you're allowing it to choose usersOnlineArray.length, which is not indexed. use Math.floor() instead.

you could move the item you don't want to match to the end of the array and then select at random an element from the array except for the last element:

//Users Array
var usersArray:Array = new Array("JoeID", "MaryID", "UserID", "JohnID", "SusanID");

//Find UserID And Push It To The End
usersArray.push(usersArray.splice(usersArray.indexOf("UserID"), 1));

//Randomly Select Companion From usersArray Except For UserID (Last Element)
var companion:String = usersArray[Math.floor(Math.random() * (usersArray.length - 1))];

trace(companion);


You are on good way, just call again your method for example

public void Something(){ string user = GetUser(); }
public string GetUser(){
   chosenUser = usersOnlineArray[Math.round((Math.random()*usersOnlineArray.length))];

  if (chosenUser == userid)
  {
    return GetUser();
  } 
return chosenUser;
}


Since Flash is same ECMA branch of languages as JavaScript and there is not a JS answer (and ActionScript is kind of extinct species) here is the answer (without recursion) in JS:

var getRandomExcept = function(elements, exceptElement){
    if(elements.length <= 0){
        return null;
    }else if(typeof exceptElement !== 'undefined' && elements.length == 1){
        if(elements[0] === exceptElement) return null;
    }

    var n = Math.floor(Math.random() * elements.length);

    if(elements[n] === exceptElement){
        // make one random trip around elements but stop (<elements.length) before the exceptElement
        n = (n + Math.floor(Math.random() * elements.length)) % elements.length;
    }
    return elements[n];
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜