Random Character generator

the next script i want to write is as the name suggests,
and i have the general setup figured out, there would be one script generating the random values and stats, while a separate script has an if function that equals the stats on the generated sheet if it’s power stat equals 0(the stat everything adds into), then it would make a copy template of itself when the button to generate is hit.

i just was wondering if anyone was willing to let me in on an easier way to do this?
PS:I’m quite amazed at how quickly my last question was Answered so i figured i’d try my luck with this!

I’m going to take a stab at what I think you mean here. I made an example character stats class that we can use to discuss:

    public class CharacterStats 
    {
        public int Strength { get; set; }
        public int Dexterity { get; set; }
        public int Intelligence { get; set; }
        public int Constitution { get; set; }

        public int TotalStatPower 
        {
            get { return Strength + Dexterity + Intelligence + Constitution; }
        }
    }

Here I’m assuming the total stat power (“power” as you mention above) is just a sum of all the statistics of a character. What it sounds like you’re thinking of doing is:

    public class RandomCharacterGenerator
    {
        public CharacterStats GenerateRandomCharacter(int maxStatPower)
        {
            return new CharacterStats
                {
                    Strength = Random.Range(0, maxStatPower),
                    Dexterity = Random.Range(0, maxStatPower),
                    Intelligence = Random.Range(0, maxStatPower),
                    Constitution = Random.Range(0, maxStatPower)
                };
        }

        public void SaveRandomCharacter()
        {
            CharacterStats characterStats;
            do
            {
                characterStats = GenerateRandomCharacter(10);
            } while(characterStats.TotalStatPower != 10);

            // then do something to save the character
        }
    }

This is going to take you a long time to generate your characters. There’s a very low probability each time that you’ll get a character with your desired total stat power, and if not, you have to throw that character away and start again. One alternative would be to assign points one by one, where the random element is which stat you assign the point to. That script would look like:

public class RandomCharacterGenerator
{
    public CharacterStats GenerateRandomCharacter(int maxStatPower)
    {
        // with this implementation, all characters generated are random,
        // but have their TotalStatPower equal to MaxStatPower
        var characterStats = new CharacterStats();
        for(int i = 0; i < maxStatPower; i++)
        {
            var statIndex = Random.Range(0, 4);
            switch(statIndex)
            {
                case 0:
                    characterStats.Strength += 1;
                    break;
                case 1:
                    characterStats.Dexterity += 1;
                    break;
                case 2:
                    characterStats.Intelligence += 1;
                    break;
                case 3:
                    characterStats.Constitution += 1;
                    break;                                             
            }
        }
        return characterStats;
    }

    public void SaveRandomCharacter()
    {
        var randomCharacter = GenerateRandomCharacter(10);

        // then do something to save the character
    }
}

If you start thinking about this script, you might realize that on average, your characters are going to tend towards a more even distribution of points across stats with this system because of how probability works. For example, it’s very unlikely that a 12 point character will have all 12 points in one stat, but much more likely that they will have 4 in each. If you’re interested in the probability theory behind this, what you’re trying to do is [choose n numbers with a fixed sum][1]. Using the algorithm from the accepted answer in that link, the following generate function will give you a more uniform distribution across stats:

        public CharacterStats GenerateRandomCharacter(int maxStatPower)
        {
            var numberOfStats = 4;
            var sortedRandomNumbers = Enumerable.Range(0, numberOfStats - 1)
                .Select(x => Random.Range(0, maxStatPower + 1))
                .Concat(new int[] { 0, maxStatPower })
                .OrderBy(x => x)
                .ToList();

            var characterStats = new CharacterStats();
            for(int i = 0; i < sortedRandomNumbers.Count - 1; i++)
            {
                var difference = sortedRandomNumbers[i + 1] - sortedRandomNumbers*;*

switch(i)
{
case 0:
characterStats.Strength = difference;
break;
case 1:
characterStats.Dexterity = difference;
break;
case 2:
characterStats.Intelligence = difference;
break;
case 3:
characterStats.Constitution = difference;
break;
}
}

return characterStats;
}
[1]: algorithm - Choosing n numbers with fixed sum - Stack Overflow