
Random numbers.
Is their a way to have a user input a number and to have that number be the size of an array and have it produce random numbers in the array that equal one number.
Lets say the user inputs 7
I am trying to figure a way to then make a function that takes that number then produce seven randomly selected numbers into an array that when added would equal a certaon number every time.
Lets say i want it to equal 50 it would produce the numbers 4, 7, 21, 8, 2, 3, 5. these add up to fifty. This is probably very confusing the way i described the problem but if you understand and know what i am talking about then your help would be greatly appreciated.
Thanks,
Nate


What you need is meory dynamics (it's very simple...)
First, you get the number, say 7. Then you have this line in your code:
Code:
int *array;
array = new int[number_that_is_from_user];
(After you have finished with array you must destroy it since it's a pointer: delete [] array;
Now, when you want to have that array full of random numbers that equal to n, things get complicated. Statistically speaking the best way might be a loop:
Code:
int sum;
while(1)
{
sum = 0;
for(int i = 0; i < number_that_is_from_user; i++)
{
array[i] = rand() % max;
sum += array[i];
}
if(sum == what_we_want)
break;
}
Only problem is that as arrays get bigger, the time needed to break from that loop gets huge!
Other simple option is that you do something like:
Code:
int max = 50, sum = 0;
for(int i = 0; i < number_that_is_from_user; i++)
{
arrray[i] = rand() % (maxsum);
sum = array[i];
}
Only problem is that array[0] has the biggest change to be biggest number, array[1] second biggest, etc. It wouldn't surprise me, that when array[0] = max then every other `random' number would be 0.
Hopefully someone else has better ideas... :)

after you get the number of random numbers to use and the value you want the numbers to add up to you can keep varying the varlues you want. Let's say you want 5 random numbers between 0 and 50 (inclusive) to add up to total of 50. That means the largest value the first number can be is 50. If the first number is 50 then all the others have to be 0. If the the first number is 3 then the other 4 can only add up to 47 so limit the next random number vary from 0 to 47. If the second number is 47 then the other 3 can only be 0. If the second number is 10 then the largest value the third number could be is 50  3  10 or 37 so limit it to between 0 and 37. If the third number is 37 then the other 2 can only be 0. If the third number is 21 then the largest number the 4th number can be is 50  3  10  21 or 16 so limit the random number generator to 0 to 16. The last number cannot be random if all the other numbers are random and the desired total is fixed, so calculate what the fifth number has to be. Now you can extrapolate this to any number of random numbers and any desired target number.

If the user input was 7:
Make the first 6 numbers random numbers with a max limit. Then, make the last number whatever is left to create the sum. Apply this concept to all numbers.

Just pointing out
Yeah, I don't think anyone explicitly mentioned (although everyone implied) that you can't do this:
Code:
any_type array_name[ variable ];
The "variable" portion really can't be a variable. It has to be a object that can be resolved during compiletime. In other words, it can be an actual number, const object, definition from #define, etc.
Even if you do know, someone browsing the thread may not. Just pointing it out!