Generate key code

I want to make a Key Code generator. I am talking about something that would allow me to generate a code like xxxx-xxxx-xxxx, but i didn’t found anything like that…

I am going to make a game where basically you solve puzzles using a computer (in game), so i wanted to make this part of the game, and i wanted to test it first in visual studio by making just a simple software just to see if it works, so i can “update” it to the game and make the system inside it.

As the player obviously needs to use the mouse and my screen have some buttons to click, i assume that i could use if (Input.GetButton("Fire1") and then make something below that, but i only found this:

Random rnd = new Random();
int month = rnd.Next(1, 13); // creates a number between 1 and 12
int dice = rnd.Next(1, 7);   // creates a number between 1 and 6
int card = rnd.Next(52);     // creates a number between 0 and 51

And i don’t get where do i insert it. Could anyone help me? I would really appreciate any sugestion.

Random values are done in Unity through the included Random class. Here you would be looking at Random.Range.

string Rand ()
{
    int rand = Random.Range (0, 10);
    return rand.ToString ();
}

void Update ()
{
    string code = Rand () + Rand () + Rand () + Rand () + "-" + Rand () + Rand () + Rand () + Rand () + "-" + Rand () + Rand () + Rand () + Rand ();
    Debug.Log (code);
}

Something like that would generate a completely random code. You can find more about the Random class here;

Give this a try … fully tested and i’ve tried to put comments wherever needed …

using UnityEngine;
using System.Collections;
using System.Text;

public class keyCode : MonoBehaviour {
	private string[] alpb, num;
	void Start () {
		num = new string[10]{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
		alpb = new string[26] {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
		Debug.Log (generateKey (3, 5)); // Just for the sake of a simple test
	}
	/*
	 * nodeCount of 3 and nodeCharCount of 5 will produce something like this
	 * yu67h-7uyh8-i8uy6
	 */
	public string generateKey(int nodeCount, int nodeCharCount){
		//Shuffle our arrays first so that every time we get a random key
		shuffleArray<string> (num);
		shuffleArray<string> (alpb);
		nodeCount = Mathf.Clamp (nodeCount, 2, 5);
		nodeCharCount = Mathf.Clamp (nodeCharCount, 3, 5);
		int numIndex = 0, alpIndex = 0, insertInt = 0;
		StringBuilder sB = new StringBuilder ();
		for (int i = 1; i <= nodeCount; i++) {
			for (int j = 0; j < nodeCharCount; j++) {
				insertInt = Random.Range (0, 2); // 0 means we will insert an alphabet in our key code and 1 means we will go with a number
				if (insertInt == 0) {
					sB.Append (alpb [alpIndex]);
					alpIndex++;
					if (alpIndex == alpb.Length) {
						alpIndex = 0;
					}
				} else {
					sB.Append (num [numIndex]);
					numIndex++;
					if (numIndex == num.Length) {
						numIndex = 0;
					}
				}
			}
			if (i < nodeCount) {
				sB.Append ("-");
			}
		}
		return sB.ToString ();
	}

	static void shuffleArray<T>(T[] arr){ // This will not create a new array but return the original but shuffled array
		for (int i = arr.Length - 1; i > 0; i--) {
			int r = Random.Range (0, i + 1);
			T tmp = arr *;*

_ arr = arr [r];_
* arr [r] = tmp;*
* }*
* }*
}

If you search for a self-verifying serial key that has the ability to expire at a certain date, have a look at my implementation over here

Of course self-verifying keys can be cracked quite easily. Make sure you read my other post carefully.