Praise the Almighty RNG

Hello!

First off, I would like to mention that I could not run the program using only Random.Range(blah). I had to add either System. or UnityEngine. as a prefix or the red squiggles would not leave me be. I chose UnityEngine. because using System. created a red squiggle underneath the “Range” part.

    void Update()
    {
        int index1 = UnityEngine.Random.Range(0, level1Passwords.Length);
        int index2 = UnityEngine.Random.Range(0, level2Passwords.Length);
        int index3 = UnityEngine.Random.Range(0, level3Passwords.Length);
        print("Level 1: " + index1);
        print("Level 2: " + index2);
        print("Level 3: " + index3);

    }

So I had my play with Update and the test proved my code was sufficient in choosing a random number.

RNG

I wanted to see what behavior the RNG would have if I inserted the random index picker into my AssignPassword() function.

    string AssignPassword()
    {
        password = "password";
        int index;
        switch (level)
        {
            case 1:
                index = UnityEngine.Random.Range(0, level1Passwords.Length);
                print("Level 1: " + level1Passwords[index]);
                password = level1Passwords[index];
                break;
            case 2:
                index = UnityEngine.Random.Range(0, level2Passwords.Length);
                print("Level 2: " + level2Passwords[index]);
                password = level2Passwords[index];
                break;
            case 3:
                index = UnityEngine.Random.Range(0, level3Passwords.Length);
                print("Level 3: " + level3Passwords[index]);
                password = level3Passwords[index];
                break;
            default:
                Debug.LogError("Invalid level number. <AssignPassword()>");
                break;
        }
        return password;
    }

Once I ran the code and tested the level selection and word assignment over and over, I could see that the RNG was working properly!

Have a nice day :slight_smile:
-GK

Privacy & Terms