Upgrade to Pro — share decks privately, control downloads, hide ads and more …

#渋谷java あなたと乱数生成とJava

#渋谷java あなたと乱数生成とJava

第十回 #渋谷java http://shibuya-java.connpass.com/event/11574/ での発表資料です。Java で乱数生成するときのお話をしました。

KOMIYA Atsushi

March 07, 2015
Tweet

More Decks by KOMIYA Atsushi

Other Decks in Programming

Transcript

  1. ͋ͳͨͱཚ਺ੜ੒ͱ Java
    ୈेճ #ौ୩java 2015-03-07
    @komiya_atsushi

    View full-size slide

  2. ͓·ͩΕ
    ʢ͓લ୭Αʁʣ

    View full-size slide

  3. ,0.*:""UTVTIJ
    !LPNJZB@BUTVTIJ

    View full-size slide

  4. Ad server and
    machine learning
    with Java

    View full-size slide

  5. Random Number
    Generator

    View full-size slide

  6. java.util.Random

    View full-size slide

  7. Random classes
    provided by JDK

    View full-size slide

  8. Random classes provided by JDK
    • java.util.Random
    • java.security.SecureRandom
    • SecureRandom.getInstance("SHA1PRNG")  
    • java.util.concurrent.ThreadLocalRandom
    • ThreadLocalRandom.current()

    View full-size slide

  9. Random classes
    provided by
    commons-math3

    View full-size slide

  10. Random classes provided by
    commons-math3
    • MersenneTwister
    • most popular (?) random number generator
    • Well44497a
    • longer period than Mersenne Twister
    • ISAACRandom
    • a fast cryptographic pseudo-random number
    generator

    View full-size slide

  11. Random classes provided by
    commons-math3
    • RandomDataGenerator supports sampling
    from various distributions
    • Beta / Binomial / Cauchy / ChiSquare /
    Exponential / F / Gamma / Hypergeometric
    / Pascal / Poisson / T / Weibull / Zipf
    • java.util.Random only supports sampling
    from Gaussian (normal distribution)

    View full-size slide

  12. Concurrency of
    Random and
    ThreadLocalRandom

    View full-size slide

  13. Concurrency of Random and
    ThreadLocalRandom
    5ISFBE-PDBM3BOEPNJTBXFTPNF

    View full-size slide

  14. Latency of 200,000,000
    times random number
    generation

    View full-size slide

  15. Latency of 200,000,000 times
    random number generation
    .FSTFOOF5XJTUFS
    JTGBTU
    3BOEPNJTOPU
    GBTU

    View full-size slide

  16. If you need…
    • high concurrency one
    • use ThreadLocalRandom
    • very long-period & high quality one
    • use Well44497a
    • long-period & fast one
    • use MersenneTwister
    • cryptographically secure one
    • use SecureRandom

    View full-size slide

  17. Thanks!
    Any questions?

    View full-size slide

  18. 8&`3&)*3*/(
    IUUQTKPCTGPSLXFMMDPNTNBSUOFXT
    PSDPOUBDUNF

    View full-size slide