I am currently working on a program that generates a series of keys based on various factors, the basics are all down however right now I am running into issues where my generator that is supposed to produce a list of keys winds up causing Unity to freeze. It would appear from my adding and removing bits that the part that causes the lag is that the program keeps generating the same initial code (what I use to generate the rest of the code) and because my system is designed to check for repeats and removes them however this results in an infinite loop. To give you a basic idea of why this shouldn’t happen there should be a 1 in 15,000,000 chance of each one being generated so I have no idea why it would cause a loop like this to occur. Bellow is the code that governs this part, my first thought is how to ensure it fully cleans itself after each pass but it shouldn’t be able to as when generating 3 or 4 it works fine but generating 10 causes unity to freeze. Is there anything I can do or must I scrap this for a new system?
public void generatevalidkeys()
{
int keycount = 0;
int KCA = 0;
int keyG = 0;
string[] keys = new string[keystobegenerated];
while (keycount < keystobegenerated)
{
Keyhalf1 = "";
while (KCA < 4)
{
keyG = Random.Range(1, 62);
if (keyG >= 1 && keyG <= 10)
{
keyG = keyG + 47;
}
else if (keyG >= 11 && keyG <= 36)
{
keyG = keyG + 54;
}
else if (keyG >= 37 && keyG <= 62)
{
keyG = keyG + 60;
}
Keyhalf1 = Keyhalf1 + System.Convert.ToChar(keyG).ToString();
KCA++;
}
KCA = 0;
Keygen();
Keyhalf1 = "";
if (System.Array.Exists(keys, element => element == finalkey) == false)
{
keys[keycount] = finalkey;
keycount++;
}
}
System.IO.File.WriteAllLines("list of keys.txt", keys);
print("Complete");
}