How to generate non-repeating Random numbers by JAVA
This article introduces the relevant knowledge of "how to generate non-repetitive random numbers in JAVA". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Code:
Import java.util.*
/ * *
* an algorithm for generating unrepeated random numbers.
*
*
*
, /
Public class T {
Public static void main (String [] args) {
/ / you can generate seeds at will, but you can't repeat them.
Int [] seed = {1,2,3,4,5,6,7,8,9}
Int [] ranArr = new int [9]
Random ran = new Random ()
/ / you can define the quantity yourself.
For (int I = 0; I < seed.length; iTunes +) {
/ / get a position
Int j = ran.nextInt (seed.length-I)
/ / get the value of that position
RanArr [I] = seed [j]
/ / put the last unused number here
Seed [j] = seed [seed.length-1-I]
}
System.out.println ("ranArr:" + Arrays.toString (ranArr))
}
}
A running result
RanArr: [6, 3, 5, 9, 7, 2, 8, 4, 1]
This is the end of the content of "how to generate non-repeating random numbers in JAVA". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!