Java generates a random number that is not duplicated "a range 0-10" or "an array int[1,3,56,14,26,22,17]"

Source: Internet
Author: User

"Java" Nonsense does not say more directly paste code:


/** * The simplest and most basic method * @param min specified range minimum * @param max specified range maximum * @param n random number of numbers */public static
		Int[] Randomcommon (int min, int max, int n) {if (n > (max-min + 1) | | Max < min) {return null;
		} int[] result = new Int[n];
		int count = 0;
			while (Count < n) {int num = (int) (Math.random () * (max-min)) + min;
			Boolean flag = true;
					for (int j = 0; J < N; j +) {if (num = result[j]) {flag = false;
				Break
				} if (flag) {result[count] = num;
			count++;
	} return result; /** * Randomly specified n in a range of numbers * Use hashset characteristics, can only hold different values * @param min specified range minimum * @param max specified range maximum * @param n Random number * @param hashset<integer> Set random number Result set */public static void Randomset (int min, int max, int n, Hashset<integer&gt ;
		Set) {if (n > (max-min + 1) | | Max < min) {return;
		for (int i = 0; i < n; i++) {//Call Math.random () method int num = (int) (Math.random () * (max-min)) + min;	Set.add (num);//The different number is deposited in hashset} int setSize = Set.size ();
		If the number of deposits is less than the number of specified builds, then the call recursively generates the remaining number of random numbers so that it loops until the specified size is reached if (SetSize < n) {randomset (min, Max, n-setsize, set);//recursive }/** * Randomly specified n not-repeated numbers in a random range * randomly generates a number into the result in an uninitialized array that is not repeated. * Replaces the number of the array to be randomly selected with the number corresponding to the subscript of the selected array (len-1) * and randomly generates the next random number from the Len-2 And so on * @param max Specify range maximum * @param min Specify range minimum * @param n random number * @return int[] Random number result set/public static int

		[] Randomarray (int min,int max,int n) {int len = max-min+1;
		if (Max < min | | | n > len) {return null;
		}//Initialize the array int[of the given range] Source = new Int[len];
		for (int i = min; i < Min+len; i++) {source[i-min] = i;
		} int[] result = new Int[n];
		Random rd = new Random ();
		int index = 0;
			for (int i = 0; i < result.length i++) {//Array 0 to (len-2) random one subscript index = Math.Abs (rd.nextint ()% len--);
			Put the random number into the result set result[i] = Source[index];
		Replaces the number of random numbers in the array to be selected with the number corresponding to the subscript of the selected array (len-1) source[index] = Source[len];
	return result;

}	/** * Randomly generating n random numbers in a discontinuous array * @param data array * @param n random number * @return int[] Random number Result set */public static int[]
		
		Randomnorepeat (int[] data1,int N) {int[] result = new Int[n];
		Random r = new Random ();
		int IRDM = 0;
			
			for (int i = 0; i < n; i + +) {IRDM = R.nextint (11-i);
			
			[3, 5, 6, 8, 9,,, result[i] = DATA1[IRDM];
			if (i = = (n-1)) {break;
			for (int j = IRDM J < 11-i-1; j + +) {Data1[j] = data1[j + 1];
	} return result;
		public static void Main (string[] args) {int[] reult1 = Randomcommon (0,10,3);
		SYSTEM.OUT.PRINTLN ("Mode 1:");
		for (int i:reult1) {System.out.print (i + ",");
		} System.out.println ();
		SYSTEM.OUT.PRINTLN ("Mode 2:");
		int[] Reult2 = Randomarray (0,10,3);
		for (int i:reult2) {System.out.print (i + ",");
		} System.out.println ();
		System.out.println ("mode 3:");
		hashset<integer> set = new hashset<integer> ();
		Randomset (0,10,3,set); for (int j:seT) {System.out.print (j + ",");
		} System.out.println ();
		SYSTEM.OUT.PRINTLN ("Mode 4:");
		Int[] Data1 = {3, 5, 6, 8, 9, 15, 18, 24, 27, 30, 32};
		int[] Reult3 = Randomnorepeat (data1,3);
		for (int i:reult3) {System.out.print (i + ","); }

	}

Post-run results:

Mode 1:
7,1,5,
Mode 2:
9,0,4,
Mode 3:
4,6,8,
Mode 4:
32,30,27,



===== Foundation is very important ==========

March 27, 2015 11:36:30

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.