最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼

 更新時(shí)間:2016年04月26日 16:27:24   作者:賣(mài)蠟筆的小新  
這篇文章主要為大家分享了java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java隨機(jī)數(shù)生成代碼,供大家參考,具體內(nèi)容如下

package com.gonvan.common.utils;
 
import java.util.*;
 
/**
 * 隨機(jī)數(shù)工具
 *
 * @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];
  }
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • SpringBoot 如何編寫(xiě)配置文件

    SpringBoot 如何編寫(xiě)配置文件

    這篇文章主要介紹了SpringBoot 編寫(xiě)配置文件的兩種方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-11-11
  • Java使用poi導(dǎo)出ppt文件的實(shí)現(xiàn)代碼

    Java使用poi導(dǎo)出ppt文件的實(shí)現(xiàn)代碼

    Apache POI 是用Java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的 Java API,Apache POI提供API給Java對(duì)Microsoft Office格式檔案讀和寫(xiě)的功能。本文給大家介紹Java使用poi導(dǎo)出ppt文件的實(shí)現(xiàn)代碼,需要的朋友參考下吧
    2021-06-06
  • Spring:spring-webmvc和spring-web有哪些區(qū)別

    Spring:spring-webmvc和spring-web有哪些區(qū)別

    這篇文章主要介紹了Spring:spring-webmvc和spring-web有哪些區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 詳解OpenCV For Java環(huán)境搭建與功能演示

    詳解OpenCV For Java環(huán)境搭建與功能演示

    這篇文章主要介紹了x詳解OpenCV For Java環(huán)境搭建與功能演示,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • springboot 集成pgsql+mybatis plus的詳細(xì)步驟

    springboot 集成pgsql+mybatis plus的詳細(xì)步驟

    集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步驟與 MyBatis 類(lèi)似,只不過(guò)在 MyBatis Plus 中提供了更多的便利功能,如自動(dòng)生成 SQL、分頁(yè)查詢(xún)、Wrapper 查詢(xún)等,下面分步驟給大家介紹springboot 集成pgsql+mybatis plus的過(guò)程,感興趣的朋友一起看看吧
    2023-12-12
  • 淺談IDEA2018打包可執(zhí)行jar包的流程

    淺談IDEA2018打包可執(zhí)行jar包的流程

    這篇文章主要介紹了淺談IDEA2018打包可執(zhí)行jar包的流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Maven默認(rèn)使用JDK1.5的問(wèn)題及解決方案

    Maven默認(rèn)使用JDK1.5的問(wèn)題及解決方案

    這篇文章主要介紹了Maven默認(rèn)使用JDK1.5的問(wèn)題及解決方案,本文給大家分享兩種方式,通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java的最大棧深度與JVM核心知識(shí)介紹

    Java的最大棧深度與JVM核心知識(shí)介紹

    這篇文章主要有兩個(gè)部分,一部分介紹JAVA的最大棧深度,第二部分介紹了JVM核心知識(shí),需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09
  • Spring Aware源碼設(shè)計(jì)示例解析

    Spring Aware源碼設(shè)計(jì)示例解析

    這篇文章主要為大家介紹了Spring Aware源碼設(shè)計(jì)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Spring Boot集成MinIO進(jìn)行文件存儲(chǔ)和管理的詳細(xì)步驟

    Spring Boot集成MinIO進(jìn)行文件存儲(chǔ)和管理的詳細(xì)步驟

    這篇文章主要介紹了Spring Boot集成MinIO進(jìn)行文件存儲(chǔ)和管理的詳細(xì)步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2025-04-04

最新評(píng)論

清涧县| 错那县| 宁南县| 漠河县| 西吉县| 新巴尔虎左旗| 通州市| 凤冈县| 永胜县| 六安市| 永泰县| 海宁市| 黄冈市| 东平县| 凌源市| 翼城县| 青海省| 鞍山市| 深州市| 托克托县| 静安区| 开鲁县| 崇信县| 石楼县| 隆昌县| 乡城县| 建昌县| 武宣县| 仁寿县| 宁陵县| 出国| 岳阳县| 浑源县| 曲阜市| 枞阳县| 翁牛特旗| 惠水县| 周宁县| 贵港市| 长武县| 利辛县|