I am running multiple threads on my application and in each thread I need to get a random Dictionary item from the account. Now I know of course once in a while you'll get the same item from a small dictionary, but this dictionary has over a thousand items and I am still getting non-unique results?
What do I mean by that? I mean it'll give random results but usually they repeat
Examples:
Picked the random username "aidan913"
Picked the random username "aidan913"
Picked the random username "abbiexox9"
Picked the random username "phelan193"
Picked the random username "pbeters92"
So It'll repeat it twice sometimes, but not actually give a fully unique item. Surely there has to be a way to get a unique item at least 9/10 times?
public KeyValuePair<int, BotInformation> GetAccount()
{
var account = new KeyValuePair<int, BotInformation>();
var rand = new Random();
var randomlyOrdered = _accounts.OrderBy(i => rand.Next());
lock (locker)
{
foreach (KeyValuePair<int, BotInformation> entry in randomlyOrdered)
{
if (!entry.Value.Usable())
continue;
account = entry;
}
}
if (!CoreUtilities.IsDefault(account)) // if it found one, update it?
{
using (var databaseConnection = Program.GetServer().GetDatabaseManager().GetConnection())
{
databaseConnection.SetQuery("UPDATE `accounts` SET `last_used` = '" +
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' WHERE `username` = '" + account.Key + "' LIMIT 1");
}
account.Value.LastUsed = DateTime.Now;
}
return account;
}