Bought a lottery for so many years, you really will generate lottery it? Brother with a look at how you can not lose control to generate lottery

Nonce is a random result of special experiments, it is necessary to use a random number of different statistical techniques, such as in a representative sample drawn from the statistical population when the process or in experimental animals assigned to different experimental groups of , the calculation of the time perhaps performing Monte Carlo simulation and the like. It generates a random number with a number of different methods, which are referred to as random number generator. Random number of important features are: the number of his later produced no connection with that of the previous number.

Today we are discussing is based on a random number expanded. As we know, in a random lottery is occurring, but in fact this is a random surface of the random row control purposes. In short are randomly generated in a high probability of

Defined set of probabilities
transformation probability set
randomly generated probability index set
by the search to find an element of
the test data to verify
Join team

table of Contents

Defined set of probability


/**
 * 定义一个连续集合
 * 集合中元素x满足:(minElement,maxElement]
 * 数学表达式为:minElement < x <= maxElement
 *
 */
public class ContinuousList {

    private double minElement;
    private double maxElement;

    public ContinuousList(double minElement, double maxElement){
        if(minElement > maxElement){
            throw new IllegalArgumentException("区间不合理,minElement不能大于maxElement!");
        }
        this.minElement = minElement;
        this.maxElement = maxElement;
    }

    /**
     * 判断当前集合是否包含特定元素
     * @param element
     * @return
     */
    public boolean isContainKey(double element){
        boolean flag = false;
        if(element > minElement && element <= maxElement){
            flag = true;
        }
        return flag;
    }

}

ContinuousList by a first class object store probabilities to set, used to indicate the sink shaft 1 on. Here Friendly reminder, here is the probability set does not necessarily require the probability is 100% probability where providers need only provide a weight on the line, we are projected to sink the spindle 2 on automatically when will once completion of the last See also the random number in the index corresponding to the piece weight. Think like a weight similar tiling in turn covered on the same line, and then randomly throw a stone looking at that piece on the line.

Construct probability factor

The probability of tiling schematic

schematic diagram

Transformation probability set

We mentioned above that provided by the supplier is a weight, FIG provide a 10,10,5,5,30,10,5,25 {}, the sum of 100 (100 is not necessary here for convenience only 100 observations) which represents means to generate the channel bit 0 is 10/100 = 10%. Now we have added a set of array { "a", "b" , "c", "d", "e", "f", "g", "h"} eight characters. Then the probability is now through configuration view of a, then generates a ~ h are respectively {10%, 10%, 5%, 5%, 30%, 10%, 5%, 25%}
How by weight into probabilities it.

  • First, the first element of the weight 10, which places the spindle 3 distributed over the range from 0 to 10
  • A second weight element 10, corresponding to the distribution range is 10 to 20
  • The third element of the weight 5, or 20 corresponding to 25 distribution
  • The fourth element of the weights 5, corresponding to the distribution range of 25 to 30
  • Fifth element weight 30, corresponding to the distribution range was 30 to 60
  • Weights sixth element 10, the distribution range corresponding to 60 to 70 compared with
  • Seventh weighting elements 5, compared with the corresponding distribution of 70 to 75
  • Eighth weighting elements 25, 75 questioned distribution corresponding to 100 essay

Distribution

The probability of randomly generated set of index


/**
 * 进行抽奖操作
 * 返回:奖品的概率list集合中的下标
 */
public int randomColunmIndex(){
    int index = -1;
    Random r = new Random();
    double d = r.nextDouble() * maxElement;  //生成0-1间的随机数
    if(d == 0d){
        d = r.nextDouble() * maxElement;     //防止生成0.0
    }
    int size = lotteryList.size();
    for(int i = 0; i < size; i++){
        ContinuousList cl = lotteryList.get(i);
        if(cl.isContainKey(d)){
            index = i;
            break;
        }
    }
    if(index == -1){
        throw new IllegalArgumentException("概率集合设置不合理!");
    }
    return index;

}

Now we sink spindle 4 has been constructed well, let generates random numbers of 0 to 1 based on the language used in each of the reader, and then press the sink shaft 5 ratio is enlarged to sink the spindle 6 on.
For example: 0.5238 randomly generated, above the sink shaft 7 length of 0.5238 corresponding to a random number 100, the generation of the sink shaft . 8 52.38.52.38 in the range of 30 to 60 range, the randomly generated so on, compared with 30 3 to 60 the corresponding index (from 0)

Find elements by cable rate

52.38 probability set corresponding to the above index is 3, here explains why the result set and the probability corresponding to the number of sets to. Because by the sink spindle 9 we generated index, the probability of the result set and the same number of sets ensures that we will get the results. By the results we have obtained to index 3 is d

Test data validation

The above describes the implementation of the principles set process improvement algorithm, the paper come Zhongjue, now we will generate 100,000 random data through the algorithm to generate the same time we add the number of statistical algorithms. Finally, we compare the actual generation of probability and probability generated under the program can validate the algorithm change.

result

Here also the readers say goodbye today, according to the probability of generating a random number to the end, if the reader random number generation principle interested, you can suggest to times soft paper owner, follow me will be the principle of analysis according to the situation.

Join team

Source provided

Personal Website

[Individual micro-channel] zxh870775401

Micro-channel public number

Micro-channel public number

Welcome to a reward

Front cover


  1. Sink shaft is integral to a probability distribution abstract rays, different probabilities ray projection on the article.

  2. Sink shaft is integral to a probability distribution abstract rays, different probabilities ray projection on the article.

  3. Sink shaft is integral to a probability distribution abstract rays, different probabilities ray projection on the article.

  4. Sink shaft is integral to a probability distribution abstract rays, different probabilities ray projection on the article.

  5. Sink shaft is integral to a probability distribution abstract rays, different probabilities ray projection on the article.

  6. Sink shaft is integral to a probability distribution abstract rays, different probabilities ray projection on the article.

  7. Sink shaft is integral to a probability distribution abstract rays, different probabilities ray projection on the article.

  8. Sink shaft is integral to a probability distribution abstract rays, different probabilities ray projection on the article.

  9. Sink shaft is integral to a probability distribution abstract rays, different probabilities ray projection on the article.

Guess you like

Origin www.cnblogs.com/zhangxinhua/p/11345989.html