开发者

Random array special

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 ge开发者_运维知识库t 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. :-)


Are you looking for a weighted random array?

Check this example:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜