微信红包算法遐想
2017-02-17 10:51
169 查看
private final AtomicLong seed;
private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;
/**
* Creates a new random number generator. This constructor sets
* the seed of the random number generator to a value very likely
* to be distinct from any other invocation of this constructor.
*/
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}
闲来无事,想如果自己设计微信红包,怎么设计呢?
因为我不是微信红包的开发人员,所以以下内容纯属虚构
先说下我的算法,每人随机一个数,然后把所有人的加起来成为分母,每个人的数是分子,分子除以分母,就是这个人占用红包的额度。
直接看代码
今天就看下源码,本人看的是JDK 1.7的
先看Random的,关键是seed因子
private final AtomicLong seed;
private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;
/**
* Creates a new random number generator. This constructor sets
* the seed of the random number generator to a value very likely
* to be distinct from any other invocation of this constructor.
*/
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}
再看下ThreadLocalRandom ,首先ThreadLocalRandom 继承了Random,仔细看接口,一些方法都是重用父类的。
用TheadLocal 实现的线程安全。
private static final ThreadLocal<ThreadLocalRandom> localRandom =
new ThreadLocal<ThreadLocalRandom>() {
protected ThreadLocalRandom initialValue() {
return new ThreadLocalRandom();
}
};
private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;
/**
* Creates a new random number generator. This constructor sets
* the seed of the random number generator to a value very likely
* to be distinct from any other invocation of this constructor.
*/
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}
闲来无事,想如果自己设计微信红包,怎么设计呢?
因为我不是微信红包的开发人员,所以以下内容纯属虚构
先说下我的算法,每人随机一个数,然后把所有人的加起来成为分母,每个人的数是分子,分子除以分母,就是这个人占用红包的额度。
直接看代码
public class Haobao { public static List<Integer> buildFen(int fen,int i){ if(fen<=i){ return null; } List<Integer> ins=new ArrayList<Integer>(i); List<Integer> cns=new ArrayList<Integer>(i); // Random r=ThreadLocalRandom.current(); Random r=new Random(); int total=0; for(int j=0;j<i;j++){ int c=r.nextInt(i*10)+1; total=total+c; ins.add(c); } BigDecimal t=new BigDecimal(total); BigDecimal f=new BigDecimal(fen); BigDecimal s=f.divide(t,4,BigDecimal.ROUND_DOWN); for(int j=0;j<i-1;j++){ int c=s.multiply(new BigDecimal(ins.get(j))).intValue(); if(c==0||(fen-c)<(i-j)){ c=1; } fen=fen-c; cns.add(c); } cns.add(fen); return cns; } public static void main(String[] args){ for(int k=0;k<10;k++){ long begin=System.currentTimeMillis(); for(int i=0;i<10*10000;i++){ List<Integer> s=buildFen(2245, 100); // System.out.println(s.toString()); } System.out.println("time:"+(System.currentTimeMillis()-begin)); } } }
Random VS ThreadLocalRandom
有人说ThreadLocalRandom 性能比Random高,有人说Random线程不安全。今天就看下源码,本人看的是JDK 1.7的
先看Random的,关键是seed因子
private final AtomicLong seed;
private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;
/**
* Creates a new random number generator. This constructor sets
* the seed of the random number generator to a value very likely
* to be distinct from any other invocation of this constructor.
*/
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}
再看下ThreadLocalRandom ,首先ThreadLocalRandom 继承了Random,仔细看接口,一些方法都是重用父类的。
用TheadLocal 实现的线程安全。
private static final ThreadLocal<ThreadLocalRandom> localRandom =
new ThreadLocal<ThreadLocalRandom>() {
protected ThreadLocalRandom initialValue() {
return new ThreadLocalRandom();
}
};
相关文章推荐
- 揭秘微信红包:架构、抢红包算法、高并发和降级方案
- Java仿实现微信红包分配算法
- 微信红包算法
- 浅谈微信红包的算法
- php仿微信红包分配算法的实现方法
- php实现的微信红包算法分析(非官方)
- 微信红包算法
- 算法与数据结构——算法题 21:微信红包(2016腾讯校招笔试题) ? 待解决
- Python版微信红包分配算法
- [算法] -- 微信红包算法
- Python微信红包算法
- 微信红包的算法实现探讨
- [置顶] 微信抢红包的算法实现(C++)
- 微信随机红包算法
- 微信红包的算法实现探讨(基于PHP)
- c# Java 微信红包算法
- 微信红包算法
- 揭秘微信红包:架构、抢红包算法、高并发和降级方案
- 微信红包的算法实现
- 代码笔记 | 微信红包算法