Java generates random numbers that are not repeated.
Package COM. cloud. sea. random; import Java. util. arrays; import Java. util. random;/*** generate random numbers that are not repeated ** @ author Henry Lee * @ version 1.0 */public class gennotrepeatrandom {public static void main (string [] ARGs) throws targetminexception {int [] arr = gennotrepeatrandom. gennum (30, 30); gennotrepeatrandom. showarr (ARR, false );} /*** generate random numbers without duplicates ** @ Param num * Number of random numbers * @ Param value * random number range * @ See note that the value cannot be less than num * @ return * @ throws targetminexception */ public static int [] gennum (INT num, int value) throws targetminexception {int [] arr = new int [num]; // Save the final generated result int Index = 0; // Status Index default = 0arr = new int [num]; If (value <num) {Throw new targetminexception ("the random number value range cannot be less than the number of random numbers generated ");} boolean result = true; while (result) {// determines whether to continue generating the random number random RD = new random (); int temprandomnum = RD. nextint (value) + 1; if (ARR [arr. length-1] = 0) {// determines whether to generate a random number and assign the value if (ishas (temprandomnum, arr, index )) {// determine whether the generated random number is repeated with the existing value in the array;} arr [index ++] = temprandomnum; // Add the generated random number without recurrence to the array} elseresult = false;} return arr ;} /*** determine whether a generated random number exists ** @ Param mm * @ Param arr * @ Param Index * @ return */Private Static Boolean ishas (INT temprandomnum, int [] arr, int index) {for (INT I = 0; I <index; I ++) {If (temprandomnum = arr [I]) {return true ;}} return false ;}/ *** showarr *** @ Param arr * @ Param issort */@ deprecatedpublic static void showarr (INT [] arr, Boolean issort) {for (Int J = 0; j <arr. length; j ++) {If (issort) {arrays. sort (ARR);} system. out. print (ARR [J] + ",") ;}} static class targetminexception extends exception {Private Static final long serialversionuid = 1l; Public String message = ""; public targetminexception (string message) {This. message = message;} @ overridepublic string getmessage () {return this. message ;}}}
Thread call
Package COM. cloud. sea. random; import Java. util. hashmap; import Java. util. map; import COM. cloud. sea. random. gennotrepeatrandom. targetminexception;/*** thread call * @ author Henry Lee **/public class threadimp1 implements runnable {string key = NULL; /*** static variable map save result * Other classes get corresponding thread execution result */public static Map <string, int []> map = new hashmap <string, int []> (); Public threadimp1 (string key) {This. key = key;} public void run () {int [] arr = NULL; try {arr = gennotrepeatrandom. gennum (1500,300 00);} catch (targetminexception e) {e. printstacktrace ();} map. put (Key, arr );}}
package com.cloud.sea.random;public class ThreadTest {public static void main(String[] args) throws InterruptedException {for (int i = 0; i < 100; i++) {ThreadImp1 timp = new ThreadImp1(i + "");Thread t = new Thread(timp);t.start();}Thread.sleep(3000);for (int i = 0; i < 100; i++) {int[] arr = ThreadImp1.map.get(i + "");for (int j = 0; j < arr.length; j++) {System.out.print(arr[j] + ",");}System.out.println();}}}