RedPacketAlgorithm.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.happy.redis;
  2. import java.util.Random;
  3. public class RedPacketAlgorithm {
  4. static Random random = new Random();
  5. static {
  6. random.setSeed(System.currentTimeMillis());
  7. }
  8. public static void main(String[] args) {
  9. long max = 20;
  10. long min = 1;
  11. long[] result = generate(100, 10, max, min);
  12. long total = 0;
  13. for (int i = 0; i < result.length; i++) {
  14. total += result[i];
  15. }
  16. // 检查生成的红包的总额是否正确
  17. // System.out.println("total:" + total);
  18. // 统计每个钱数的红包数量,检查是否接近正态分布
  19. int count[] = new int[(int) max + 1];
  20. for (int i = 0; i < result.length; i++) {
  21. count[(int) result[i]] += 1;
  22. }
  23. // for (int i = 0; i < count.length; i++) {
  24. // System.out.println("" + i + " " + count[i]);
  25. // }
  26. }
  27. /**
  28. * 生产min和max之间的随机数,但是概率不是平均的,从min到max方向概率逐渐加大。
  29. * 先平方,然后产生一个平方值范围内的随机数,再开方,这样就产生了一种“膨胀”再“收缩”的效果。
  30. *
  31. * @param min
  32. * @param max
  33. * @return
  34. */
  35. static long xRandom(long min, long max) {
  36. return sqrt(nextLong(sqr(max - min)));
  37. }
  38. /**
  39. *
  40. * @param total
  41. * 红包总额
  42. * @param count
  43. * 红包个数
  44. * @param max
  45. * 每个小红包的最大额
  46. * @param min
  47. * 每个小红包的最小额
  48. * @return 存放生成的每个小红包的值的数组
  49. */
  50. public static long[] generate(long total, int count, long max, long min) {
  51. if (count * max < total) {
  52. // System.out.println("最大红包钱数 * 红包个数 < 总钱数");
  53. System.exit(-1);
  54. }
  55. long[] result = new long[count];
  56. long average = total / count;
  57. long a = average - min;
  58. long b = max - min;
  59. //
  60. // 这样的随机数的概率实际改变了,产生大数的可能性要比产生小数的概率要小。
  61. // 这样就实现了大部分红包的值在平均数附近。大红包和小红包比较少。
  62. long range1 = sqr(average - min);
  63. long range2 = sqr(max - average);
  64. for (int i = 0; i < result.length; i++) {
  65. // 因为小红包的数量通常是要比大红包的数量要多的,因为这里的概率要调换过来。
  66. // 当随机数>平均值,则产生小红包
  67. // 当随机数<平均值,则产生大红包
  68. if (nextLong(min, max) > average) {
  69. // 在平均线上减钱
  70. // long temp = min + sqrt(nextLong(range1));
  71. long temp = min + xRandom(min, average);
  72. result[i] = temp;
  73. total -= temp;
  74. } else {
  75. // 在平均线上加钱
  76. // long temp = max - sqrt(nextLong(range2));
  77. long temp = max - xRandom(average, max);
  78. result[i] = temp;
  79. total -= temp;
  80. }
  81. }
  82. // 如果还有余钱,则尝试加到小红包里,如果加不进去,则尝试下一个。
  83. while (total > 0) {
  84. for (int i = 0; i < result.length; i++) {
  85. if (total > 0 && result[i] < max) {
  86. result[i]++;
  87. total--;
  88. }
  89. }
  90. }
  91. // 如果钱是负数了,还得从已生成的小红包中抽取回来
  92. while (total < 0) {
  93. for (int i = 0; i < result.length; i++) {
  94. if (total < 0 && result[i] > min) {
  95. result[i]--;
  96. total++;
  97. }
  98. }
  99. }
  100. return result;
  101. }
  102. static long sqrt(long n) {
  103. // 改进为查表?
  104. return (long) Math.sqrt(n);
  105. }
  106. static long sqr(long n) {
  107. // 查表快,还是直接算快?
  108. return n * n;
  109. }
  110. static long nextLong(long n) {
  111. return random.nextInt((int) n);
  112. }
  113. static long nextLong(long min, long max) {
  114. return random.nextInt((int) (max - min + 1)) + min;
  115. }
  116. }