4

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.

3 Answers 3

3

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;
}
4
  • Yes, this is the way I would do it. You do need to make sure to check that there's at least 1 other user online though, otherwise you will have infinite recursion.
    – StapleGun
    Jan 27, 2011 at 0:44
  • gotta use comparative == not assignment = Jan 27, 2011 at 3:19
  • 1
    I have updated it, but I don't know what language is he talking about, just algorithm how to solve problem Jan 27, 2011 at 13:12
  • This is recursive, you shouldn't use it because it's slower, the best way to do this is using a do..while loop
    – user216441
    Jan 27, 2011 at 14:50
3

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

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];
};
1
  • Note: this works only if elements are not repeated in the list
    – mPrinC
    Sep 11, 2015 at 8:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.