Create numbers within an array that add up to a set amount
Posted
by RussellDias
on Stack Overflow
See other posts from Stack Overflow
or by RussellDias
Published on 2010-05-08T11:03:17Z
Indexed on
2010/05/08
11:08 UTC
Read the original article
Hit count: 166
php
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
© Stack Overflow or respective owner