How do I generate 30 random numbers between 1-9, that all add up to 200 (or some arbitrary N), in C#?
I\'m trying to generate a string of digits that can add togethe
I think this is the simplest way to do it, so it may lack some sophistication however it will get you there.
String output = "";
int sum = 0;
int result = 200; //enter the "end number"
Random r = new Random();
while (sum != result) {
int add;
if ((result - sum) > 10)
{
add = r.Next(1, 10);
}
else
{
add = r.Next(result - sum + 1);
}
sum += add;
output += add.ToString() + " + ";
}
output = output.Remove(output.Length - 2);
Console.WriteLine(output);
Hope it helps!