Generating random numbers in java

1. Use the random method to generate random numbers.

Generating random numbers in Java language is relatively simple because there are ready-made methods to use. In mathematics, the Java language provides a method called random. This method allows the system to generate random numbers. However, by default, the generated random numbers range from 0 to less than 1. Although the range of random number generation is relatively small, it can not meet the daily needs. For example, you may need to generate integer random numbers in your daily work. In fact, as long as this method is handled flexibly, you can get any range of random numbers.

For example, we can generate a random number by a random method and then multiply the result by 10. The random number generated at this time is a number greater than or equal to 0 and less than 10. Then use Int method to convert (the decimal will be removed, that is, only the integer part will be obtained, without rounding). Finally, you can get an integer random number from 0 to 9. Its implementation method is very simple, that is, the original random method is modified according to the following format: (int)(Math. Random()* 10)。 In fact, we can also extend this method to generate random numbers in any range. Just change this 10 to n, such as (int)(Math. Random()*n)。 At this point, the application will generate a random number greater than or equal to 0 and between small and n. If n is set to 5, it will generate an integer random number between 0 and 5. If this is written as a method with parameters, then as long as the user inputs the maximum value needed to generate random numbers, this method can be used to generate random numbers within the specified range. Define your own tool library in Java.

Sometimes a programmer may need to generate a random even or odd number within a specific range. Can this be achieved in this way? The answer is yes. If the program now needs to generate an even number in the range of 1- 100. How to achieve this at this time? First of all, you need to generate a random number between 0 and 99 (as for why it is 99 here, you will know the reason after a patient look). To achieve this requirement, it is very simple. It can be realized by the following statement: I = 1+(int) (Mathematics. random () * 100)。 Where (int) (Mathematics. Random()*99) generates an integer random number between 0 and 99. Then add 1 to generate a random integer between 1 and 100. Then assign the generated random number to the variable I, but at this time the generated random number is both even and odd. What programmers need now is a random even number. Then we can add an if judgment statement after it. Divide this random number by 2. If there is no remainder (or the remainder is 0), it means that this random number is even. Just return it directly. If the remainder it returns is not zero, it means it is odd. We just need to add 1 to make it even and return it. Note that for the above random number generation, I used the range from 0 to 99, and then added 1 to make it a random number from 1 to 100. The end result is a random even number between 1 and 100. In fact, if you want the range to be random and odd, you need to modify the above statement slightly. Java: Change your world and mine.