English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Generating Java random numbers is simple, it can be done through
Random rand = new Random(47); System.out.println(rand.nextInt());
can be generated, or generated as follows:
double d = Math.random();
Of course, the former in the code uses a fixed seed47Therefore, the value is the same each time, and it can also be used
Random rand = new Random(); System.out.println(rand.nextInt());
For the code2Then the random number generated is a double type.
Let's talk about3method, there is a requirement at present to generate4For random numbers, which are used for generating SMS registration codes, random numbers need to be used, so the code3To implement this. If the code between them is used, the result will not meet the conditions, so the following method is used to implement it:
//Method 1 Random rand = new Random(); for (int i = 0; i < 4; i++{ System.out.print(Math.abs(rand.nextInt() % 10)) } //Random numbers are generated through rand.next, as there may be negative numbers, Math.abs is used to take the absolute value, and then modulo is taken.10,and the generated results are in}}10within //Method two Random rand = new Random(); for (int i = 0; i < 4; i++{ System.out.print(rand2.nextInt(10)) } //The above are directly generated using the api10within
This is a Java random number module I wrote recently, encapsulating various practical methods related to randomness, which I specially bring to share.
There is nothing high-tech here; the function names also indicate their usage, so a simple comment is enough. 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(); //obtains a random integer within a given range public static int getRandomNum(int smallistNum, int BiggestNum) { return (Math.abs(random.nextInt()) % (BiggestNum-smallistNum+1))+smallistNum; } //obtains a random boolean value public static Boolean getRandomBoolean() { return (getRandomNum(0,1) ==== 1); } //obtains a random number in the range of 0~1a floating-point number public static float getRandomFloatIn_1()) { return (float) getRandomNum(0,1000)/1000; } //obtains 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); } //returns 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 from the given array public static <T> T getElement(T[] t) { int index = getRandomNum(0,t.length); - 1); return t[index]; } //Return a random element from 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(); } }
Summary
This is the full content of the random number generation module code sharing about Java programming in this article. I hope it will be helpful to everyone. Those who are interested can continue to read other related topics on this site. Welcome to leave a message if there are any shortcomings. Thank you for your support to this site!
Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. The website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w.3Please report any violations by email to codebox.com (replace # with @) and provide relevant evidence. Once verified, the website will immediately delete the suspected infringing content.