distribute amount

Prev Next

Function Names

distribute amount

Description

This function distributes an amount provided in the 1st function parameter across a set with number of elements as specified in teh 2nd set. Different options are available to distribut that value: Following rules are supported:

Rule Description
even The amount is distributed evenly across all element. Example: 3 over 4 elements result in { 0.75, 0.75, 0.75, 0.75 }.
row wise The amount is distributed as whole numbers across the elements. Example: 14.5 over 4 elements result in { 4, 4, 3.5, 3 }.
random The amount is randomly distributed across the elements. Example: 11.5 over 6 elements could result in { 3, 0, 4, 2.5, 3, 2 }. Note that cases occur where at least one element can stay zero.
row wise random The amount is distributed bottom up across the elements, with the last ones on top distributed evenly. Example: 11.5 over 5 elements could result in { 2, 3, 2, 3.5, 2 }. This function ensures that no two consecutive random values will affect the same set element.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

1, 2

Parameters

No.TypeDescription
1.
input
numeral Amount to distribute

This amount will be distributed across the set elements so the sum of all elements equals to this amount.

2.
input
numeral Element count

The value must be 1 or bigger and is used to create the resulting parameter with specified size.

Alt. 3.
input
string Distribution rule

See the table above for valid rules.

Default value: even

Return value

TypeDescription
set Distributed Amount

set with the amount distriuted across the elements

Examples

       echo( "even            : ", distribute amount( 14, 5, even ) );
       echo( "row wise        : ", distribute amount( 14, 5, row wise ) );
       echo( "row wise random : ", distribute amount( 14, 5, row wise random ) );
       echo( "random          : ", distribute amount( 14, 5, random ) );     

Output

even            : {2.8,2.8,2.8,2.8,2.8}
row wise        : {3,3,3,3,2}
row wise random : {3,2,3,3,3}
random          : {0,1,5,2,6}
Try it yourself: Open LIB_Function_distribute_amount.b4p in B4P_Examples.zip. Decompress before use.