Create numbers within an array that add up to a set amount
- by RussellDias
I'm fairly new to PHP - programming in general. So basically what I need to accomplish is, create an array of x amount of numbers (created randomly) who's value add up to n:
Lets say, I have to create 4 numbers that add up to 30. I just need the first random dataset. The 4 and 30 here are variables which will be set by the user.
Essentially something like
x = amount of numbers;
n = sum of all x's combined;
create x random numbers which all add up to n;
$row = array(5, 7, 10, 8) // these add up to 30
Also, no duplicates are allowed.
I need the values with an array. I have been messing around with it sometime, however, my knowledge is fairly limited. Any help will be greatly appreciated.
Cheers