Random number generator in Java: Random,threadlocalrandom,securerandom

Source: Internet
Author: User
Tags random seed uuid

random number generator in Java: Random,threadlocalrandom,securerandom

In the article

The Random is: Java.util.Random,
Threadlocalrandom namely: Java.util.concurrent.ThreadLocalRandom
SecureRandom namely: Java.security.SecureRandom


is Q:random thread-safe?
A:random is thread-safe, but may have low performance under multithreading.
Reference:
Http://docs.oracle.com/javase/7/docs/api/java/util/Random.html
Http://stackoverflow.com/questions/5819638/is-random-class-thread-safe

Why is q:threadlocalrandom so fast?
A: In fact, this look at the source code will know. Because random uses a lot of CAS classes, Threadlocalrandom is not used at all.

Q: Why not use random in the case of high-intensity requirements?

A: Especially in the case of generating a verification code, do not use random because it is linear and predictable. Remember that there is a news about a gambling site, in order to illustrate its fairness, public its source code, as a result of the random number of predictable vulnerabilities were attacked. Therefore, in the case of high security requirements, the use of securerandom should be used.

Update 2014-4-22:http://news.cnblogs.com/n/206074/

Reference: http://www.inbreak.net/archives/349

Q: Theoretically, the random number generated by the computer is pseudo-random number, so how to produce high-intensity random number?
A: There are two important factors for generating high-intensity random numbers: seeds and algorithms. Of course the algorithm can have a lot of, but how to choose the seed is very important factor. Like random, its seed is system.currenttimemillis (), so its random number is predictable. So how do you get an approximate random seed? Here's a very chic idea: Collect information about your computer, such as keyboard input time, CPU clock, memory usage status, hard disk free space, IO delay, number of processes, number of threads, etc. to get an approximate random seed. In this case, in addition to the theory of the possibility of cracking, in fact, basically no possibility of being cracked. In fact, today's high-intensity random number generators are implemented in this way.
For example, a random number generator under Windows:
Http://blogs.msdn.com/b/michael_howard/archive/2005/01/14/353379.aspx
Http://msdn.microsoft.com/en-us/library/aa379942%28VS.85%29.aspx
/dev/random under Linux:
Http://zh.wikipedia.org/wiki//dev/random
According to SecureRandom's Java doc, it is possible to use/dev/random to implement UNIX-like systems.


Some of the other interesting stuff:
The fastest method of generating UUID with low security requirements (note that the intensity is not high and may be duplicated):

[Java]View PlainCopy  
    1. New UUID (Threadlocalrandom.current (). Nextlong (), Threadlocalrandom.current (). Nextlong ());

See on a website, forget the source.


Randomly generate a function that produces a random number?
Can a random number generator be used to generate a series of random code, and then as a new random number generator? It seems that the intensity is passing, it doesn't seem to make sense.

Random number generator in Java: Random,threadlocalrandom,securerandom

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.