mardi 15 novembre 2022

How exactly does one make random numbers add up to a declared sum?

I'm confused as to how exactly I would make 9 random numbers add to whatever number the user may input. Let's say the user inputs "200" as the number, how do I make it so that I could get 9 random numbers add up exactly to 200?

Obviously, the code below doesn't work the way I want it to because it's literally just 9 random numbers that don't add up to a specific number. I just have no idea how to get this built properly.

public static void RandomStats()
{
    Random RandomClass = new Random();

    int[] intRandomStats = {
        RandomClass.Next(0, 101), 
        RandomClass.Next(0, 101), 
        RandomClass.Next(0, 101), 
        RandomClass.Next(0, 101), 
        RandomClass.Next(0, 101), 
        RandomClass.Next(0, 101), 
        RandomClass.Next(0, 101), 
        RandomClass.Next(0, 101), 
        RandomClass.Next(0, 101)
    };

    // ...
}



Aucun commentaire:

Enregistrer un commentaire