The online racing simulator
Issue with selecting random users
I am having problems with an InSim I am making it is using c# but I have come across something and I just can't figure out a solution.

What I am trying to do is make a drag InSim application (I know there is one on this forum already but i want to create my own for a certain reason), I have created the select 3 random users from the server to stage and drag but what happens is it selects the same user 3 times or 2 times like Host vs Host vs Host or Host vs RacerX vs Host and it will not stay on the same 3 users it swaps every second so it just flickers with different names, I cant figure out a way to code it so it doesn't select the same user more then once and so it doesn't change every second, I was wondering if there was anyone with an idea on how to fix this or point me in the right direction.

I have added a screen shot with one example on what happens.

http://lightning-servers.com/uploads/example.jpg
#2 - Silox
Well, if you want to select 3 unique users on a server with 2 people only, you'll have a problem.

So, firstly try to get some more testers online, I think 4 or 5 will be enough.

I suppose you just pick a random PLID from an array? If so, you should make a do ... until loop and check every nickname if it's not listed in the array from 'choosen draggers'.

It would help us to find a solution if you posted some bit of code here.
Quote from Silox :I suppose you just pick a random PLID from an array.

That's a good idea, and you also brought up a good point about selecting 3 users when there are only 2 players online AND in race.

So, first your function should do a sanity check. Make sure there are three racers in the race that you can choose from. If there is not, don't run the function, abort here.

Get an array of all of the users in the race.

while number of racers selected is less then 3
{
Get array size for the random number's max value. We always use zero for our min value as that's the array keys min value.

get the random number for the arrays min value (zero) and max value (size of racers array).

get the array key number from the returned random number, and place that into a our selected racers array.

remove the array key number from our racers array.

increment number of races selected.
}
return selected racers array.
Well i had 4 people in helping me test and it does the same thing with selecting the same user twice ill try doing this array and ill post what happens thanks for the help
Based on Dygear's answer, presuming you have a dictionary/map of User objects named _users, it would be something like this in C#:

// Imaginary user map keyed by the UCID.
var _users = new Dictionary<int, User>();
var _rand = new Random();

User GetRandomUser()
{
return _users.Values[_rand.Next(_users.Count - 1)];
}

List<User> GetRandomUsers(int count)
{
if (count < _users.Count)
{
throw new ArgumentException("not enough users");
}

var selectedUsers = new List<User>();
while (selectedUsers.Count < count)
{
var user = GetRandomUser();
if (!selectedUsers.Contains(user))
{
selectedUsers.Add(user);
}
}
return selectedUsers;
}

Note: This code isn't tested, but I have determined it to be correct.

You would call it like this:

var randomUsers = GetRandomUsers(3); // Get 3 random users

-
(DarkTimes) DELETED by DarkTimes

FGED GREDG RDFGDR GSFDG