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

℡╲_俬逩灬. 提交于 2020-06-27 15:55:14

问题


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


回答1:


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;
}



回答2:


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



回答3:


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];
};


来源:https://stackoverflow.com/questions/4811267/how-to-pick-random-element-in-an-array-avoiding-except-if-its-a-certain-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!