Generate a big digit with only 1 2 3 and 4,How to generate a big number that only countains 0 to 3

I’m creating a Genetics system to my game based in Space Station 13, but genetics in the game have like 100 variables per player so i was working on a optimized version that could put everything in just a few, my idea is to make number composed only of 1s to 4s, like: 1423(the numbers are gonna be 16digits but i’m using small ones here so it’s easier to understand), the reason it’s numbers from 1 to 4 is because:

  • **1 means Cromossome TA **
  • **2 means Cromossome AT **
  • **3 means Cromossome CG **
  • 4 means Cromossome GC

The variables are gonna be translated to a string when being read by the user but i want to keep them as this numbers since it’s way faster and simpler to have a 16 digit number(Ulong) compared to having 16 different arrays for every single disease, the problem is that if I use the random functions like Mathf.Random it generates just a random number, which could be like 5948 and I can’t have any number other than 1 2 3 and 4, So basically:
I want to have a function that can generate big numbers composed only of number between 1 and 4, for example: 1423142324132232, it’s a big number because it’s a Ulong type
In the end they are going to be translated back to string to be understand by the player, like 1432 = TAGCCGAT, also if possible a way to get only a certain digit of a number

,I’m trying to make a “Genetics” system for a game of mine, every DNA would have a 16-digit number(a Ulong variable), the problem is that I can only have numbers from 1 to 4, because in my system:

  • 1 = TA cromossome
  • 2 = AT cromossome
  • 3 = CG cromossome
  • 4 = GC cromossome

So if i simply use Mathf.Random it could generate something like 19382… and I can’t have number like 9 or 8 in the variables, in this case i want something like: 1243432313212312, as you can see, a 16-digit number which contains only number from 1 to 4, not related to the question but if possible also a way to read and change a specific digit, like see only the fifth digit in the number to translate it to a string, instead of reading the entire number

Hello there,

This may not be the best way to do this, but it works:

    public static int GenerateRandXDigitCode(int nbOfDigits, int min, int max)
        string toReturn = "";
        for (int i = 0; i < nbOfDigits; ++i)
            int rand = UnityEngine.Random.Range(min, max);
            toReturn += rand.ToString();

        return int.Parse(toReturn);

nbOfDigits is the length of your number, min is the inclusive minimum (1 in your case), and max is the exclusive maximum (5 in your case).

Hope that helps!



This function will generate the number

    //Generate a number numDigits long where each digit is a random number between 0 and 4 inclusive 
    ulong GenerateNumber(int numDigits)
        ulong retval = 0;
        for (int i = 0; i < numDigits; i++)
            ulong digit = (ulong)Random.Range(0, 5); //This will return an int between 0-4 inclusive

            //This loop is the equivalent of doing: digit * (10 ^ i)
            for(int j = 0; j < i; j++) //Add i number of trailing zeros
                digit *= 10;
            retval += digit;//Add to the existing number
        return retval;

And this function can be used to determine the digit at particular place in a larger number

     //Get the digit occupying a particular place in a number 
    //Note: This is zero indexed and right to left
    //i.e. place 0 is the ones column, place 1 is the tens column, etc.  
    int GetDigit(ulong fullNumber, int place)
        ulong retval = fullNumber;
        //Loop through removing all digits to the right of the one we want
        for(int i = 0; i < place; i++)
            retval /= 10;
        //Remove all digits to the left of the one we want
        retval %= 10; 
        return (int)retval;