Generate non-repeating random numbers java
java can use the method in the Math class to generate random numbers. If it is not random, you can use List for judgment storage. (Recommended tutorial: java tutorial )
1. Call the random() method in the Math class below java.lang to generate random numbers
Create a new file with the suffix name java, and the file name is MyRandom. Write the following code in this class:
public class MyRandom { public static void main(String[] args) { int radom = (int)(Math.random()*10); System.out.println(radom); } }
Math.random() //Produces 0~1 a random decimal between.
To generate an integer between 0 and 9 is: (int)(Math.random()*10);
To generate an integer between 1 and 10 you can write: (int)(Math.random()*10 1);
And so on: to generate a number between 0 and n, it should be written as: Math.random()*n;
For example: Generate an int type array with a length of 50, and insert numbers between 0-50 into it randomly, and they cannot be repeated.
2. Use the contains method of List to make repeated judgments
public class MyRandom { public static void main(String[] args) { int[] intRandom = new int[50]; List mylist = new ArrayList(); //生成数据集,用来保存随即生成数,并用于判断 Random rd = new Random(); while(mylist.size() < 50) { int num = rd.nextInt(51); if(!mylist.contains(num)) { mylist.add(num); //往集合里面添加数据。 } } for(int i = 0;i <mylist.size();i++) { intRandom[i] = (Integer)(mylist.get(i)); } } }
The above is the detailed content of Generate unique random numbers java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools