根据给定的一个概率数组随机分配概率
程序员文章站
2022-06-01 20:37:48
在程序中有时候需要为一组数据根据自定义的概率数组随机分配,如图: 概率数组: 平均分配的效果图: 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 };
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
上一篇: 激光打印机提示”该文档未能打印,本地下层文档”,怎么解决?
下一篇: Oracle跨用户查询实例教程