java随机数生成具体实现代码

 更新时间:2016年04月26日 16:27:24   作者:卖蜡笔的小新  
这篇文章主要为大家分享了java随机数生成具体实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java随机数生成代码,供大家参考,具体内容如下

package com.gonvan.common.utils;
 
import java.util.*;
 
/**
 * 随机数工具
 *
 * @author yuerzm
 *   
 */
public final class LotteryAliasMethod {
 
  /**
   * The random number generator used to sample from the distribution.
   */
  private final Random  random;
 
  /**
   * The alias tables.
   */
  private final int[]   alias;
 
  /**
   * The probability tables.
   */
  private final double[] probability;
 
  /**
   * Constructs a new AliasMethod to sample from a discrete distribution and
   * hand back outcomes based on the probability distribution.
   * <p/>
   * Given as input a list of probabilities corresponding to outcomes 0, 1,
   * ..., n - 1, this constructor creates the probability and alias tables
   * needed to efficiently sample from this distribution.
   *
   * @param probabilities
   *      The list of probabilities.
   */
  public LotteryAliasMethod(List<Double> probabilities) {
    this(probabilities, new Random());
  }
 
  /**
   * Constructs a new AliasMethod to sample from a discrete distribution and
   * hand back outcomes based on the probability distribution.
   * <p/>
   * Given as input a list of probabilities corresponding to outcomes 0, 1,
   * ..., n - 1, along with the random number generator that should be used as
   * the underlying generator, this constructor creates the probability and
   * alias tables needed to efficiently sample from this distribution.
   *
   * @param probabilities
   *      The list of probabilities.
   * @param random
   *      The random number generator
   */
  public LotteryAliasMethod(List<Double> probabilities, Random random) {
    /* Begin by doing basic structural checks on the inputs. */
    if (probabilities == null || random == null)
      throw new NullPointerException();
    if (probabilities.size() == 0)
      throw new IllegalArgumentException("Probability vector must be nonempty.");
 
    /* Allocate space for the probability and alias tables. */
    probability = new double[probabilities.size()];
    alias = new int[probabilities.size()];
 
    /* Store the underlying generator. */
    this.random = random;
 
    /* Compute the average probability and cache it for later use. */
    final double average = 1.0 / probabilities.size();
 
    /*
     * Make a copy of the probabilities list, since we will be making
     * changes to it.
     */
    probabilities = new ArrayList<Double>(probabilities);
 
    /* Create two stacks to act as worklists as we populate the tables. */
    Deque<Integer> small = new ArrayDeque<Integer>();
    Deque<Integer> large = new ArrayDeque<Integer>();
 
    /* Populate the stacks with the input probabilities. */
    for (int i = 0; i < probabilities.size(); ++i) {
      /*
       * If the probability is below the average probability, then we add
       * it to the small list; otherwise we add it to the large list.
       */
      if (probabilities.get(i) >= average)
        large.add(i);
      else
        small.add(i);
    }
 
    /*
     * As a note: in the mathematical specification of the algorithm, we
     * will always exhaust the small list before the big list. However,
     * due to floating point inaccuracies, this is not necessarily true.
     * Consequently, this inner loop (which tries to pair small and large
     * elements) will have to check that both lists aren't empty.
     */
    while (!small.isEmpty() && !large.isEmpty()) {
      /* Get the index of the small and the large probabilities. */
      int less = small.removeLast();
      int more = large.removeLast();
 
      /*
       * These probabilities have not yet been scaled up to be such that
       * 1/n is given weight 1.0. We do this here instead.
       */
      probability[less] = probabilities.get(less) * probabilities.size();
      alias[less] = more;
 
      /*
       * Decrease the probability of the larger one by the appropriate
       * amount.
       */
      probabilities.set(more, (probabilities.get(more) + probabilities.get(less)) - average);
 
      /*
       * If the new probability is less than the average, add it into the
       * small list; otherwise add it to the large list.
       */
      if (probabilities.get(more) >= 1.0 / probabilities.size())
        large.add(more);
      else
        small.add(more);
    }
 
    /*
     * At this point, everything is in one list, which means that the
     * remaining probabilities should all be 1/n. Based on this, set them
     * appropriately. Due to numerical issues, we can't be sure which
     * stack will hold the entries, so we empty both.
     */
    while (!small.isEmpty())
      probability[small.removeLast()] = 1.0;
    while (!large.isEmpty())
      probability[large.removeLast()] = 1.0;
  }
 
  /**
   * Samples a value from the underlying distribution.
   *
   * @return A random value sampled from the underlying distribution.
   */
  public int next() {
    /* Generate a fair die roll to determine which column to inspect. */
    int column = random.nextInt(probability.length);
 
    /* Generate a biased coin toss to determine which option to pick. */
    boolean coinToss = random.nextDouble() < probability[column];
 
    /* Based on the outcome, return either the column or its alias. */
    return coinToss ? column : alias[column];
  }
 
}

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

相关文章

  • SpringBoot 如何编写配置文件

    SpringBoot 如何编写配置文件

    这篇文章主要介绍了SpringBoot 编写配置文件的两种方法,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下
    2020-11-11
  • Java使用poi导出ppt文件的实现代码

    Java使用poi导出ppt文件的实现代码

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java对Microsoft Office格式档案读和写的功能。本文给大家介绍Java使用poi导出ppt文件的实现代码,需要的朋友参考下吧
    2021-06-06
  • Spring:spring-webmvc和spring-web有哪些区别

    Spring:spring-webmvc和spring-web有哪些区别

    这篇文章主要介绍了Spring:spring-webmvc和spring-web有哪些区别,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • 详解OpenCV For Java环境搭建与功能演示

    详解OpenCV For Java环境搭建与功能演示

    这篇文章主要介绍了x详解OpenCV For Java环境搭建与功能演示,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • springboot 集成pgsql+mybatis plus的详细步骤

    springboot 集成pgsql+mybatis plus的详细步骤

    集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等,下面分步骤给大家介绍springboot 集成pgsql+mybatis plus的过程,感兴趣的朋友一起看看吧
    2023-12-12
  • 浅谈IDEA2018打包可执行jar包的流程

    浅谈IDEA2018打包可执行jar包的流程

    这篇文章主要介绍了浅谈IDEA2018打包可执行jar包的流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Maven默认使用JDK1.5的问题及解决方案

    Maven默认使用JDK1.5的问题及解决方案

    这篇文章主要介绍了Maven默认使用JDK1.5的问题及解决方案,本文给大家分享两种方式,通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • Java的最大栈深度与JVM核心知识介绍

    Java的最大栈深度与JVM核心知识介绍

    这篇文章主要有两个部分,一部分介绍JAVA的最大栈深度,第二部分介绍了JVM核心知识,需要的朋友可以参考下面文章的具体内容
    2021-09-09
  • Spring Aware源码设计示例解析

    Spring Aware源码设计示例解析

    这篇文章主要为大家介绍了Spring Aware源码设计示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Spring Boot集成MinIO进行文件存储和管理的详细步骤

    Spring Boot集成MinIO进行文件存储和管理的详细步骤

    这篇文章主要介绍了Spring Boot集成MinIO进行文件存储和管理的详细步骤,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2025-04-04

最新评论