vendredi 13 février 2015

Assign random integer to items in array and summa should ever be a fixed number

this is my problem


I've a variable number of items in an array



array (x, y, z, ..., n)


And i've a fixed total assigned to a variable



$min_int = 0;
$max_int = 8;
$total = 8;


I need to assign to every item in the array a random int from $min_int to $max_int and the total of the items in the array has to be always =8.


For example:



array (red, green, blue);
$min_int = 0;
$max_int = 8;
$total = 8;


the result could be



Array
(
[0] => 3
[1] => 5
[2] => 0
)


OR



Array
(
[0] => 3
[1] => 3
[2] => 2
)


I would develop in javascript.


Any suggestions please?





Aucun commentaire:

Enregistrer un commentaire