views:

38

answers:

1

how make special random array. i have array(1=>35,2=>25,3=>40). how make possibility that array show special elements. Ex. If i want get 100 elements from array. it will be

35/100 +-10 - must bee 1 element,
25/100 +-10 = must be 2 element,
40/100 +-10 - must be 3 element.

Elements must be random, but most elements +-10. i know its possible make by this code:

$a = array_fill(1, 35, 1);
$b = array_fill(36, 60, 2);
$c = array_fill(61, 100, 3);

array looks like array(35 elements with value 1, 25 elements with value 2, 40 elements with value 3) and using merge and array_rand i will get my code. But i don't want this code. it will be create 100 items. need optimization this code. it's possible. help. :-)

A: 

Are you looking for a weighted random array?

Check this example:

http://20bits.com/articles/random-weighted-elements-in-php/

nico
Yes very thanks. :-)
Valdas
@Valdas This is not the same you asked... You definitely did not ask for a categorical distribution because of your two constraints – that the variation is only +- 10 and that the sum of all is 100.
Artefacto