- Home /
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
Answer by Legend_Bacon · May 13, 2019 at 04:47 PM
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!
Cheers,
~LegendBacon
Oh and in your case, you probably want to use longs ins$$anonymous$$d of ints :)
Couple of notes about this solution
UnityEngine.Random.Range(int $$anonymous$$, int max) is exclusive when it comes to the max value, the largest value it will return is (max - 1) Edit:Whoops just noticed you mentioned this!
Due to the immutable nature of strings this will create quite a bit of garbage which might be an issue depending on where/how often this function is used. This could be mitigated by using the StringBuilder class.
Answer by cdnDave · May 13, 2019 at 07:54 PM
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;
}
Ins$$anonymous$$d of the inner for loop, adding the digit to the left, why don't you multiply retval by 10, and then, add digit, "concatenating" it to the right of the number?
Your answer
Follow this Question
Related Questions
Display random element from dictionary 1 Answer
Semi-Random Or Engine 1 Answer
Is Random.Range() Really Maximally Inclusive? 4 Answers
My Script Wont work? 2 Answers
How to randomize answers? 1 Answer