0

I am new to Unity and my requirement is to generate a random string based on regular expression in C#. My code is working well in Console Application, however, I am getting error for Fare namespace in UnityEditor. I explored on Internet and different forums, however, I didn't found any solution. Below is my code.

string GenerateString()
{
    string pattern = "^x[a2]{9,15}";
    var xeger = new Xeger(pattern);
    string generatedString = xeger.Generate();
    generatedString = xeger.Generate();
    return generatedString;
}

The error I am getting in UnityEditor is as follows;

Assets\Scripts\PlayerController.cs(3,7): error CS0246: The type or namespace name 'Fare' could not be found (are you missing a using directive or an assembly reference?)

I have tried the solution provided here for Nuget packages and Unity errors. The solution provided here works fine for "Fare" namespace however, UnityEngine.UI starts problem and visual studio shows that this namespace is not installed. Furthermore, I have tried to install NugetForUnity it didn't work too.

My Requirement is: Generate random collectibles and each collectible should have a randomly generated string with length ranging from 9 to 15 characters using 3 alphabets. The first alphabet should be x. The second and third one should be derived from your first name and registration number – the first letter of the first name and last digit of your registration number. E.g. If my name is Abcd and my registration number is AB20-12786, then alphabets for my strings would be (x, a, 6).

Any other suggestion will be highly appreciated for string generation for the above requirement. Thanks in Advance.

4
  • 1
    In your code snippet you are not using any Fare (or is that where Xeger comes from which I also don't know) ... could you show your complete code? Commented Jul 6, 2020 at 10:29
  • @derHugo Xeger comes from Fare. I am sorry I didn't add full code. I have just added that function which generates random string. Commented Jul 6, 2020 at 10:32
  • So you say using Fare; .. is this a library you imported into your project? Commented Jul 6, 2020 at 10:35
  • Yes, this is a library and I have imported it in my project. Commented Jul 6, 2020 at 10:43

1 Answer 1

2

I don't know that library Fare or how exactly you imported it into your project.


However, if I understand your underlying problem correctly what you want is

  • one fix "alphabet" that only contains 'x'
  • one "alphabet" that only contains the first character of a name
  • one "alphabet" that only contains the last digit of a certain string/number

I say "alphabet" because the way you describe it each of your "alphabets" anyway only seems to contain exactly one single character.

  • Then build a new string with length between 9 and 15 and randomly taking one of the 3 given characters

So something like

private System.Random random = new System.Random();

string GenerateString(string name, string number)
{
    // Get you 3 available "alphabets" or better said single characters
    var availableCharacters = new char[3];
    availableCharacters[0] = 'x';
    // Get first char of name
    availableCharacters[1] = name[0];
    // Get last char of number
    availableCharacters[2] = number[number.Length-1];

    // Create a char array with random length between 9 and 15
    // NOTE that the last parameter of Random.Next is EXCLUSIVE
    var randomChars = new char[random.Next(9, 16)];
    // Then fill it with randomly picked characters from availableCharacters
    for(var i = 0; i < randomChars.Length; i++)
    {
        // pick random value from the given 3 chars
        // again NOTE that the last parameter of Random.Next is EXCLUSIVE
        randomChars[i] = availableCharacters[random.Next(0, availableCharacters.Length)];
    }
    
    // Finaly convert that random char array into your output string
    return new string(randomChars);
}

Edit

now that we know that x should always be the first character and then not used anymore:

private System.Random random = new System.Random();

string GenerateString(string name, string number)
{
    // Get you 2 available "alphabets" or better said single characters
    var availableCharacters = new char[2];
    // Get first char of name
    availableCharacters[0] = name[0];
    // Get last char of number
    availableCharacters[1] = number[number.Length-1];

    // Create a char array with random length between 9 and 15
    // NOTE that the last parameter of Random.Next is EXCLUSIVE
    var randomChars = new char[random.Next(9, 16)];
    // Fill in the first fix character x
    randomChars[0] = 'x';
    // Then fill the rest with randomly picked characters from availableCharacters
    // NOTE that we are starting the loop at index 1 this time since we already filled 0 with x
    for(var i = 1; i < randomChars.Length; i++)
    {
        // pick random value from the given 2 chars
        // again NOTE that the last parameter of Random.Next is EXCLUSIVE
        randomChars[i] = availableCharacters[random.Next(0, availableCharacters.Length)];
    }
    
    // Finaly convert that random char array into your output string
    return new string(randomChars);
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you @derHugo for your cooperation. Everything is working perfectly however, "Alphabet" "x" must come in the start only after that combination of only two characters should come.
you said your alphabets would be x, a, 6 .. you didn't mention that x should anyway only be the first character ... you can simply hardcode it then ;) ... See the update at the bottom
One more problem with your code is that in a loop of 5 iterations for the first two iterations it generates random string however, string generated in 3rd, 4th and 5th iteration is equal to the string generated in 2nd iteration.
What do you mean? How exactly did you test that?
I have tested it by calling your function in the "start" function and writing output to the Unity console using Debug.Log
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.