标签:++ vat img push 获取 生成 returns alert ram
在程序中有时候需要为一组数据根据自定义的概率数组随机分配,如图:
概率数组:
平均分配的效果图:
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 };
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 #endregion
标签:++ vat img push 获取 生成 returns alert ram
原文地址:https://www.cnblogs.com/freedom-ly/p/9899968.html