I have the following code, and need to get a random item that contains the string "Region_1" from the dictionary.
public static Dictionary<int, Tuple<string, int, CurrencyType>> ItemArray = new Dictionary<int, Tuple<string, int, CurrencyType>>()
{
{ 0xB3E, new Tuple<string, int, CurrencyType>("Region_1", 1500, CurrencyType.Fame) }
};
public static int GenerateItemID(string ShopID)
{
var GeneratedItem = ItemArray.ElementAt(new Random().Next(0, ItemArray.Count)).Key;
}
How do I select this?
It's not really possible to this all that efficiently...
First create a static Random
at class level... this will prevent non-random behaviour if you run the query frequently over a short period of time... (it's seeded from a discrete clock)
static Random rnd = new Random();
then:
var item = ItemArray.Values
.Where(t => t.Item1 == ShopID)
.OrderBy(_ => rnd.Next())
.FirstOrDefault()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.