需要做一个给每个人参加活动的人瓜分固定奖池的游戏币,有平均瓜分和随机分2种分配方案。
平均分很简单,就是总money/总person取整数就行,无论整除不整除,不整除有结余也无所谓。
根据一个时间段的参与人数设置一个总游戏币数,所有人进行瓜分,每个人最少获得一个币,同时可以无限次重新随机计算,计算出来的列表进行分页展示,而且支持对单个人获得币进行修改,然后统一生成奖励。
涉及的redis操作(简化)
jedis.lpush(key,value); //存放key,value value是可变类型,LPUSH mylist a b c
jedis.lrange(key, startNumber, endNumber);//查询
jedis.lset(key,index,value); //根据index进行修改
jedis.lindex(key, index); //根据当前index获取list中的值
每个人最少得到1游戏币,所以先给每个人分配一个游戏币,保证在随机下每个人所得不为0
随机每个人所得范围在(0-money / people * 2)
同时利用Random产生的随机数固定种子下产生的随机序列数相同来排查记录。
当前利用微信抢红包不需要一次性将整个分布计算下来,可以在每个人领红包的时候再进行计算
利用excel工具直观查看随机数分布是否满足预想
废话不多说,贴代码:
@Testpublic void randTestTest(){randTest(1000,100);}public static void randTest(int sum,int count){List<Integer> list = new ArrayList<>();sum = sum-count*1;Random random = new Random();long factor = System.currentTimeMillis();System.out.println("固定种子是:"+factor);random.setSeed(factor);//当random设置相同的种子时,随机序列数相同for(int i = 0;i<count;i++){int rand = rand(sum, count-i, list,random);sum = rand;}int summoneny = 0;int min (0);int max =-1;for(Integer entry :list){summoneny = summoneny+entry.intValue();min = Math.min(min,entry.intValue());max = Math.max(max,entry.intValue());}System.out.String());System.out.println(min);System.out.println(max);}/**** @param money 剩余的钱* @param people 剩余的人数* @param l 红包列表* @return*/public static int rand(int money, int people, List<Integer> l ,Random random) {if (people == 1) {int red = money;l.add(red+1);return 0;}int min = 0;int max = money / people * 2;int red = Int(max);red = red <= min ? min : red;l.add(red+1);int remain = money-red;return remain;}
分布图展示,看起来后者分布更加均匀
利用钱找人,有可能一个人没有被找过一次,当然可以先给每个人先分一个游戏币,然后剩下的币找人,上代码
@Testpublic void rand1Test(){Random random = new Random();long factor = System.currentTimeMillis();System.out.println("固定种子是:"+factor);random.setSeed(factor);//当random设置相同的种子时,随机序列数相同int[] ints = rand1(1000, 100, random);int a = 0;int min =ints[0];int max =-1;for (int integer : ints) {a+=integer;min = Math.min(min,integer);max = Math.max(max,integer);}System.out.String(ints));System.out.println(a);System.out.println(min);System.out.println(max);}/**** @param money 总钱* @param count 剩余的人数* @return*/public int[] rand1(int money, int count ,Random random) {int[] nums = new int[count];for(int i= 0;i<money;i++){int person = Int(count);nums[person] +=1;}return nums;}
public static void sortListString(List list){System.out.println("排序前:"String());Collections.sort(list, new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {return Integer.valueOf(o1.substring(o1.indexOf(":")+1,o1.length()))pareTo(Integer.valueOf((o2.substring(o2.indexOf(":")+1,o2.length()))));}});System.out.println("排序后:"String());}
本文发布于:2024-01-29 04:17:18,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170647304112636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |