The generation of java random numbers is relatively simple, and can be passed
Random rand = new Random(47); System.out.println(rand.nextInt());
Generate, or it can be generated by:
double d = Math.random();
Of course, since the former uses a fixed seed 47 in the code, the value is the same every time, and you can also use it
Random rand = new Random(); System.out.println(rand.nextInt());
For code 2, the random number of double is generated.
Let’s talk about the method of 3. There is currently a requirement that 4 is a random number for generation of SMS registration codes. Then, random numbers need to be used, so code 3 is used to implement it. If the code is used between the two, the result cannot meet the conditions, then it is implemented in the following ways:
//Method one Random rand = new Random(); for (int i = 0; i < 4; i++){System.out.print(Math.abs(rand.nextint() % 10));}//The above generates random numbers through rand.next. Because there may be negative numbers, use Math.abs to get the absolute value, and then take the modulo 10. The result is within 10. //Method two Random rand = new Random(); for (int i = 0; i < 4; i++){System.out.print(rand2.nextint(10));}//The above uses the API to directly generate random numbers within 10A JAVA random number module I recently wrote encapsulates various practical methods related to randomness and is specially used to share.
There is no high-tech thing here, and the purpose of function naming can be seen, so I will just comment it briefly. If you have any questions, please leave a message.
Source code (RandomSet.java):
import java.awt.Color;import java.util.Collection;import java.util.Iterator;import java.util.Random;public class RandomSet {static Random random = new Random();//Get a random integer of a given range public static int getRandomNum(int smalllistNum,int BiggestNum) {return (Math.abs(random.nextint())%(BiggestNum-smallistNum+1))+smallistNum;}//Get a random boolean public static Boolean getRandomBoolean() {return (getRandomNum(0,1) == 1);}//Get a random floating point number between 0~1 public static float getRandomFloatIn_1() {return (float)getRandomNum(0,1000)/1000;}//Get a random color public static Color getRandomColor() {float R = (float)getRandomNum(0,255)/255;float G = (float)getRandomNum(0,255)/255;float B = (float)getRandomNum(0,255)/255;return new Color(R,G,B);}//Return a boolean value with a certain probability public static Boolean getRate(int rate) {if(rate<0 || rate > 100) {return false;} else {if(getRandomNum(0,100)<rate) {return true;} else {return false;}}}//Return a random element in the given array public static <T> T getElement(T[] t) {int index = getRandomNum(0,t.length - 1);return t[index];}//Return a random element in the given collection public static <T> T getElement(Collection<? extends T> c) {int atmp = getRandomNum(0,c.size() - 1);Iterator<? extends T> iter = c.iterator(); while(atmp > 0) {atmp--;iter.next();}return iter.next();}}Summarize
The above is all the content of this article about Java programming a random number generation module code sharing, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!