在程式中有時候需要為一組數據根據自定義的概率數組隨機分配,如圖: 概率數組: 平均分配的效果圖: JS 例子: 1 function gailv(num) { 2 3 4 $(".slider_num").text(0); 5 $(".slider_num_input").val(0); 6 7 v ...
在程式中有時候需要為一組數據根據自定義的概率數組隨機分配,如圖:
概率數組:
平均分配的效果圖:
JS 例子:
1 function gailv(num) { 2 3 4 $(".slider_num").text(0); 5 $(".slider_num_input").val(0); 6 7 var gls = [0, 0, 0, 0, 0, 0, 0, 0.02, 0.03, 0.04, 0.04, 0.04, 0.04, 0.07, 0.09, 0.10, 0.09, 0.06, 0.08, 0.10, 0.09, 0.06, 0.02, 0.01], //概率 8 sliders = [], 9 multi = 0, 10 remainder = 0, 11 takeRemainder = 0, 12 slis = null, 13 lineSize = 200, //單行最大任務數 14 nTaskNum = lineSize * 17, 15 lineTote = lineSize * gls.length; //任務總數 16 17 18 if (num > lineTote) { 19 layer.alert('任務總數最大為' + lineTote); 20 num = lineTote; 21 } 22 23 //獲取與dom相對應的數組 24 for (var a = 0; a < gls.length; a++) { 25 sliders.push(0); 26 } 27 28 //隨機分配任務 29 for (var i = num; i > 0; i--) { 30 31 slis = random(sliders, gls); 32 33 if (sliders[slis] >= lineSize) { 34 for (var j = 7; j < sliders.length ; j++) { 35 if (sliders[j] < lineSize) { 36 sliders[j]++; 37 break; 38 } 39 } 40 sliders[slis] = lineSize; 41 } 42 else { 43 sliders[slis] += 1 44 } 45 46 } 47 48 //獲取多餘任務數 49 if (num > nTaskNum) { 50 multi = num - nTaskNum; 51 } 52 53 //分配多餘任務數 54 if (multi > 0) { 55 remainder = parseInt(multi / 7); 56 takeRemainder = multi % 7; 57 58 $.each(sliders, function (index, item) { 59 if (index <= 6) { 60 sliders[index] = remainder; 61 } 62 }) 63 if (takeRemainder > 0) { 64 for (var l = 0; l < takeRemainder; l++){ 65 sliders[l]++; 66 } 67 } 68 } 69 70 //渲染dom 71 for (var n = 0; n < sliders.length; n++) { 72 $("#eq .slider_con").eq(n).find(".slider_num").text(sliders[n]); 73 } 74 75 $.each(sliders, function (index, item) { 76 try { 77 $("#eq .slider_con").eq(index).find(".slider_num_input").val(item); 78 $("#eq .slider_con").eq(index).find("span").slider("value", item); 79 } catch (e) { 80 } 81 }); 82 } 83 84 function random(arr1, arr2) { 85 var sum = 0, 86 factor = 0, 87 random = Math.random(); 88 89 for (var i = arr2.length - 1; i >= 0; i--) { 90 sum += arr2[i]; // 統計概率總和 91 }; 92 93 random *= sum; // 生成概率隨機數 94 95 for (var i = arr2.length - 1; i >= 0; i--) { 96 factor += arr2[i]; 97 if (random <= factor) 98 return i; 99 }; 100 return null; 101 };View Code
C# 例子:
1 #region 按照給定的nums總數,根據概率數組分配 2 /// <summary> 3 /// 按照給定的nums總數,根據概率數組分配 4 /// </summary> 5 /// <param name="nums">總數</param> 6 /// <param name="gls">概率數組</param> 7 /// <returns>分配好的int[]</returns> 8 public static int[] AgeProbability(int nums, double[] gls = null) 9 { 10 double[] gls1 = { 0, 0, 0, 0, 0, 0, 0.02, 0.03, 0.04, 0.04, 0.04, 0.04, 0.07, 0.09, 0.10, 0.09, 0.06, 0.08, 0.10, 0.09, 0.06, 0.02, 0.01, 0 }; 11 if (gls == null) 12 gls = gls1; 13 14 int[] sliders = new int[gls.Length]; 15 for (int i = 0; i < sliders.Length; i++) 16 { 17 sliders[i] = 0; 18 } 19 Random ran = new Random(); 20 for (int i = nums; i > 0; i--) 21 { 22 var slis = rans(sliders, gls, ran); 23 sliders[slis] += 1; 24 } 25 return sliders; 26 } 27 28 private static int rans(int[] sliders, double[] gls, Random ran) 29 { 30 double sum = 0; 31 double actor = 0; 32 double r = ran.NextDouble(); 33 34 for (int i = gls.Length - 1; i >= 0; i--) 35 { 36 sum += gls[i]; //統計概率總和 37 } 38 r *= sum; // 生成概率隨機數 39 for (int i = gls.Length - 1; i >= 0; i--) 40 { 41 actor += gls[i]; 42 if (r <= actor) 43 return i; 44 } 45 return 0; 46 } 47 48 49 #endregionView Code