Order number consumption code generation (linear congruence algorithm)

    Use the linear congruence algorithm to produce the code, and obtain the order number from the code pool each time. The storage uses redis cache to speed up the acquisition efficiency. The linear congruence algorithm is used to produce the code, and the order number is obtained from the code pool each time. Storage uses redis cache to speed up retrieval efficiency.

1 Linear Congruence

    Linear congruential generator is
a pseudo-random sequence generator of the form Xn = (aXn-1 = b) mod m, where Xn is the nth number of the sequence and Xn-1 is the n- 1th number of the sequence Number of variables, a, b, m are constants, a is a multiplier, b is an increment, m is a modulus, and the key or seed is the initial value X0.
    The period of such a generator will not exceed m. If a, b, and m are all optional, then the generator will be a maximal period generator (sometimes called maximum length) with period m.
(For example, b is a prime number relative to m.) Care is needed in choosing constants to ensure that the largest period is found.
The advantages of linear congruential generators are that they are fast and require very few operations per bit.

2 Linear congruence to generate pseudo-random numbers

    In the program, the linear congruence algorithm is used to generate random numbers. The specific code is as follows:

long c = 7L;
long a = 21L;
int vs = 50000;

public synchronized   void generatorCustomCode(){
        String[]values = new String[vs];
        for (int i = 0; i < vs; i++) {
            value = (a * value + c) % m;
            code_value = String.format(fc, value);
            values[i] = code_value;
        }
    }
    return setOperations.pop(code + flow);
}


3 Random number and salt for transformation

In this way, the random numbers generated each time, even if there are repetitions, will not be repeated after the transformation is added.

public synchronized   void generatorCustomCode(){
        String[]values = new String[vs];
        for (int i = 0; i < vs; i++) {
            value = (a * value + c) % m;
            code_value = String.format(fc, value);
            key = String.format(fk, c_value);
            if(c_value<1000) {
                code_value = code_value.substring(0, 2) + key.substring(0, 1) + code_value.substring(2, 5)
                        + key.substring(1, 2) + code_value.substring(5, 8) + key.substring(2) + code_value.substring(8);
            }else{
                code_value = code_value.substring(0, 2) + key.substring(0, 1) + code_value.substring(2, 5) + key.substring(1, 2) + code_value.substring(5, 8)
                        + key.substring(2, 3) + code_value.substring(8, 9)+ key.substring(3) + code_value.substring(9);
            }
            values[i] = code_value;
        }
    }
    return setOperations.pop(code + flow);
}


4 Examples

An example of using this method to generate a service number, complete code

public synchronized   String generatorCustomCode(){
    String code = "mc:ye:order:code:custom:";
    String key  = code + "key";
    ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
    SetOperations<String, String> setOperations = redisTemplate.opsForSet();
    String flow = (String)valueOperations.get(key);
    //初始化KEY
    if(flow==null){
        flow = "100";
        valueOperations.set(key, flow);
    }
    //初始化原始码池
    Long size = setOperations.size(code + flow);
    if(size==null||size<=0){
        Long startTime = DateUtils.getMillis();
        logger.info("消费码码池耗尽,重新生成码开始-------");
        valueOperations.increment(key,1);
        flow = (String)valueOperations.get(key);
        long c_value = Long.valueOf(flow);
        long m = 1000000000L;
        String fc = "%09d";
        String fk = "%03d";
        if(c_value >= 1000){
            m = 1000000000L;
            fc = "%011d";
            fk = "%04d";
        }

        long value = c_value;
        String code_value = "";
        String[]values = new String[vs];
        for (int i = 0; i < vs; i++) {
            value = (a * value + c) % m;
            code_value = String.format(fc, value);
            key = String.format(fk, c_value);
            if(c_value<1000) {
                code_value = code_value.substring(0, 2) + key.substring(0, 1) + code_value.substring(2, 5)
                        + key.substring(1, 2) + code_value.substring(5, 8) + key.substring(2) + code_value.substring(8);
            }else{
                code_value = code_value.substring(0, 2) + key.substring(0, 1) + code_value.substring(2, 5) + key.substring(1, 2) + code_value.substring(5, 8)
                        + key.substring(2, 3) + code_value.substring(8, 9)+ key.substring(3) + code_value.substring(9);
            }
            values[i] = code_value;
        }
        batchAdd(code + flow,values);
        logger.info("生成消费码,码池生成结束,耗时:{} ms",DateUtils.getMillis()-startTime);
    }
    return setOperations.pop(code + flow);
}


 

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324081192&siteId=291194637