欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java抽奖算法第二例

程序员文章站 2024-03-13 16:22:21
本文实例为大家分享了java抽奖算法,供大家参考,具体内容如下 1. 算法分析  根据概率将奖品划分区间,每个区间代表一个奖品,然后抽取随机数,反查落在那个区...

本文实例为大家分享了java抽奖算法,供大家参考,具体内容如下

1. 算法分析
 根据概率将奖品划分区间,每个区间代表一个奖品,然后抽取随机数,反查落在那个区间上,即为所抽取的奖品。 

Java抽奖算法第二例

2. 代码
核心算法 

public class arithmetic {
  // 放大倍数
  private static final int mulriple = 1000000;

  public int pay(list<prize> prizes) {
    int lastscope = 0;
    // 洗牌,打乱奖品次序
    collections.shuffle(prizes);
    map<integer, int[]> prizescopes = new hashmap<integer, int[]>();
    map<integer, integer> prizequantity = new hashmap<integer, integer>();
    for (prize prize : prizes) {
      int prizeid = prize.getprizeid();
      // 划分区间
      int currentscope = lastscope + prize.getprobability().multiply(new bigdecimal(mulriple)).intvalue();
      prizescopes.put(prizeid, new int[] { lastscope + 1, currentscope });
      prizequantity.put(prizeid, prize.getquantity());

      lastscope = currentscope;
    }

    // 获取1-1000000之间的一个随机数
    int luckynumber = new random().nextint(mulriple);
    int luckyprizeid = 0;
    // 查找随机数所在的区间
    if ((null != prizescopes) && !prizescopes.isempty()) {
      set<entry<integer, int[]>> entrysets = prizescopes.entryset();
      for (map.entry<integer, int[]> m : entrysets) {
        int key = m.getkey();
        if (luckynumber >= m.getvalue()[0] && luckynumber <= m.getvalue()[1] && prizequantity.get(key) > 0) {
          luckyprizeid = key;
          break;
        }
      }
    }

    if (luckyprizeid > 0) {
      // 奖品库存减一
    }

    return luckyprizeid;
  }
}

prize bean

public class prize {

  /**
   * 奖品唯一标示
   */
  private integer prizeid;

  /**
   * 中奖概率
   */
  private bigdecimal probability;

  /**
   * 奖品数量
   */
  private integer quantity;

  public integer getprizeid() {
    return prizeid;
  }

  public void setprizeid(integer prizeid) {
    this.prizeid = prizeid;
  }

  public bigdecimal getprobability() {
    return probability;
  }

  public void setprobability(bigdecimal probability) {
    this.probability = probability;
  }

  public integer getquantity() {
    return quantity;
  }

  public void setquantity(integer quantity) {
    this.quantity = quantity;
  }

}

3. 测试

prize1概率: 5% 
prize2概率: 10% 

prize3概率: 15% 
prize4概率: 20% 

prize5概率: 50% 


public class test {
  public static void main(string[] args) {
    list<prize> prizes = new arraylist<prize>();
    prize prize1 = new prize();
    prize1.setprizeid(1);
    prize1.setprobability(new bigdecimal(0.05));
    prize1.setquantity(1);
    prizes.add(prize1);

    prize prize2 = new prize();
    prize2.setprizeid(2);
    prize2.setprobability(new bigdecimal(0.10));
    prize2.setquantity(10);
    prizes.add(prize2);

    prize prize3 = new prize();
    prize3.setprizeid(3);
    prize3.setprobability(new bigdecimal(0.15));
    prize3.setquantity(20);
    prizes.add(prize3);

    prize prize4 = new prize();
    prize4.setprizeid(4);
    prize4.setprobability(new bigdecimal(0.20));
    prize4.setquantity(50);
    prizes.add(prize4);

    prize prize5 = new prize();
    prize5.setprizeid(5);
    prize5.setprobability(new bigdecimal(0.50));
    prize5.setquantity(200);
    prizes.add(prize5);

    int prize1gettimes = 0;
    int prize2gettimes = 0;
    int prize3gettimes = 0;
    int prize4gettimes = 0;
    int prize5gettimes = 0;
    arithmetic arithmetic = new arithmetic();
    int times = 1000;
    for (int i = 0; i < times; i++) {
      int prizeid = arithmetic.pay(prizes);
      switch (prizeid) {
        case 1:
          prize1gettimes++;
          break;
        case 2:
          prize2gettimes++;
          break;
        case 3:
          prize3gettimes++;
          break;
        case 4:
          prize4gettimes++;
          break;
        case 5:
          prize5gettimes++;
          break;
      }
    }
    system.out.println("抽奖次数" + times);
    system.out.println("prize1中奖次数" + prize1gettimes);
    system.out.println("prize2中奖次数" + prize2gettimes);
    system.out.println("prize3中奖次数" + prize3gettimes);
    system.out.println("prize4中奖次数" + prize4gettimes);
    system.out.println("prize5中奖次数" + prize5gettimes);
  }
}

结果:

 Java抽奖算法第二例

通过1000次抽取,我们看出算法精度还是很高的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。