In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to solve the random.nextInt pit in java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Java random.nextInt 's pit
The following code
Random random = new Random (); Integer code = random.nextInt (len); very simple two sentences of code, two points to pay attention to
First: the value of nextInt is [0jue n), not including n. If it is a random list, send the size of list directly, and you don't have to worry about subscript exceeding the limit.
Api description:
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
Second: when the amount of data is small, the repetition probability of nextInt is relatively high. For example, now that there is a list of size 6, I want to display four at random without repeating it. The correct thing to do is to remove the subscript to the object each time you get a random number. In this way, it doesn't matter if the random is repeated, because after the subscript corresponding data is removed, the same subscript corresponds to different objects.
Don't go through the list as I did before, and then randomly take the subscript and then repeat it. Sometimes you get four, sometimes you don't. For example, the subscript will appear 5, 1, 1, 1, 1, 1, 2, and 1. In that case, there will only be three list in the end.
There has been no doubt that this code is the problem, suspected that the interface is unstable or incomplete data and so on. I have been checking the log to see the parameters passed and returned by the API. The result is that I don't have a deep understanding of nextInt. I feel that randomInt is random and does not repeat, but it turns out that I think too much.
Java random.nextInt () is not random
Recently, I have been studying algorithms and writing some Mini Program, one of which is an exercise in introduction to algorithms: an implementation of the RANDOM (a, b) process, which only calls RANDOM (0,1). As functions of an and b, what is the expected running time of your program?
Many people have given the answer to this question on the Internet.
I have also written an algorithm myself, but the topic of this article is not aimed at this problem, but the implementation of RANDOM (0,1). I just started using random.nextInt (0,1) to take random 0 and 1, and also tested their "randomness". The code is as follows:
/ / using Random.nextInt (2) to obtain 0 Random 1 random number / / acquisition probability is 0.5, but not random public int randomBase0 () {Random r = new Random (); return r.nextInt (2);}
Using for cycle 100 million times, the 0 and 1 obtained are about the same, and the probability of getting 0 and 1 is 0.5. But then I ran into trouble. I wrote a method to implement RANDOM (a, b), such as RANDOM (0,3), and the result was:
There are 8335 in 0
One has 825.
2 there are 42
3 there are 798
My algorithm is
Extend b-a+1 to the minimum power series of 2, if the number is 5, take 8 (2 ^ 3), if it is 16, take 16 (2 ^ 4), and then use the divide-and-conquer algorithm to obtain the number between an and b, in which the number greater than b needs to be removed and reacquired.
I thought my algorithm for writing RANDOM (a, b) was wrong. Later, I wrote an algorithm, which is used by many people on the Internet, in which the randomly generated 0 and 1 are combined into binary numbers and then converted into decimal numbers, which are valid within an interval, and refetching is excluded beyond this interval.
The result obtained by this algorithm is the same as that of the first algorithm. I had to print out the binary number of the second algorithm to find out the reason, and found a problem, that is, the probability of continuous occurrence of 0 or 1 is higher than that of the intersection of 0 and 1. I think since 0 and 1 are generated randomly, then the probability of the two generation should be the same.
Therefore, I have come to the conclusion that the probability of using Random.nextInt (0,1) to obtain random numbers is not random, because the probability of generating the same continuous 0 or 1 is different from the probability of generating crossover 0 and 1, and the former is greater than the latter, although the number ratio of 0 and 1 is equal, in other words, the events of getting 0 and 1 by this method are not independent.
Verify the following / / check Random.nextInt (2), get two random digits public int randomBaseS () {String s = new String (new StringBuffer (). Append (getBoolean ()) .append (getBoolean (); if ("00" .equals (s)) {return 0;} else if ("01" .equals (s)) {return 1;} else if ("10" .equals (s)) {return 2;} else {return 3 }} / / get random number binary string public String getBoolean () {return new String (new Integer (randomBase0 ()). ToString ());}
Using the for loop 10000 times, the count result is as follows:
There are 4145 "00".
There are 928 "01".
There are 905 "10"
There are 4022 "11".
So which algorithm can meet the requirements? As follows:
/ / using Math.random () to obtain 0 return Math.random 1 random number / / the acquisition probability is 0. 5, and the random number () {return Math.random () > 0. 5 minutes 1 random 0;}
Based on the above method, the integer between [0,3] is obtained by for loop, and the result is as follows:
There are 2525 in 0
There are 2551 in one
2 there are 2433
3 there are 2491
The above is all the content of this article "how to solve the random.nextInt pit in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.