I have many HashSetthat contain different index exceptions. I combine these hashes into one big one HashSetdepending on the input. For testing purposes, I also ported HashSetto analogues List.
- The sole purpose of
HashSetand Listis to exclude indexes from random number generation.
What I do in case of List:
list2 = new List<int>();
for (int d = 0; d < list1.Count; d++)
{
if (dicCat4[30].ContainsKey(list1[d]))
{
list2.AddRange(dicCat4[30][list1[d]]);
}
}
rand = 2 * RandString.Next(0 / 2, (dicCat[30].Count) / 2);
while (list2.Contains(rand))
{
rand = 2 * RandString.Next(0 / 2, (dicCat[30].Count) / 2);
}
// action with random
As you can see, all exceptions (indexes) are combined into one list using AddRange(). Using the method, Contains()we check whether a random number is in the list or not.
The same operation can be performed using a HashSet:
excludehash = new HashSet<int>();
for (int d = 0; d < list1.Count; d++)
{
if (dicCat4[30].ContainsKey(list1[d]))
{
excludehash.UnionWith(dicCat3[30][list1[d]]);
}
}
rand = 2 * RandString.Next(0 / 2, (dicCat[30].Count) / 2);
while (excludehash.Contains(rand))
{
rand = 2 * RandString.Next(0 / 2, (dicCat[30].Count) / 2);
}
// action with random
in this case, instead, AddRange()I use a method UnionWith()to combine HashSetindex exceptions.
, - List !, , , HashSet . , hog - HashSet UnionWith().
: HashSet ? ( : Contains(rand) hashset, , UnionWith())
P.S. :
static Dictionary<int, Dictionary<int, HashSet<int>>> dicCat3;
static Dictionary<int, Dictionary<int, List<int>>> dicCat4;
EDIT: hardcore
int inter = list1.Count;
int cco = 0;
while (inter != cco)
{
cco = 0;
rand = 2 * RandString.Next(0 / 2, (dicCat[30].Count) / 2);
for (int d = 0; d < list1.Count; d++)
{
if (dicCat4[30].ContainsKey(list1[d]))
{
if (!dicCat3[30]][list1[d]].Contains(rand))
{
cco++;
}
else
{
break;
}
}
else
{
cco++;
}
}
}