Java classic algorithm _ 035

Source: Internet
Author: User
Package WZS. arithmetics; // random number public class test {public static void main (string [] ARGs) {int [] test = getrandomintwithout1_plicate (0, 10, 5); // arrays. sort (TEST); For (int I: Test) {system. out. print (I + "") ;}}/*** returns an array of non-repeated random numbers [min, max) * @ Param minimum Random Number * @ Param maximum Random Number * @ Param array length * @ return an array without repeated values */public static int [] getrandomintwithout1_plicate (INT min, int Max, int size) {in T [] result = new int [size]; // array container int arraysize = max-min; // array range: int [] intarray = new int [arraysize]; // initialize the array for (INT I = 0; I <intarray. length; I ++) {intarray [I] = I + min;} // obtain an array that is not repeated for (INT I = 0; I <size; I ++) {int c = getrandomint (Min, Max-I); int Index = C-min; swap (intarray, index, arraysize-1-I ); result [I] = intarray [arraysize-1-I];} return result;}/*** swap Array In the two digit locations * @ Param array * @ Param x * @ Param y */Private Static void swap (INT [] array, int X, int y) {int temp = array [X]; array [x] = array [y]; array [y] = temp;}/*** get a random number int [min, max) * @ Param minimum random value * @ Param maximum random value * @ return random number int */public static int getrandomint (INT min, int max) {int result = min + new double (math. random () * (max-min )). intvalue (); return result;}/*** get a random character * @ Return a random char with the range ASCII 33 (!) To ASCII 126 (~) */Public static char getrandomchar () {// from ASCII code 33 to ASCII code 126 int firstchar = 33 ;//"! & Quot; int lastchar = 126; // & quot ;//"~ "Char result = (char) (getrandomint (firstchar, lastchar + 1); return result ;} /*** get a random length string * @ Param length the length of the string * @ return a string filled with random char */public static string getrandomstring (INT length) {// include ASCII code from 33 to 126 stringbuffer result = new stringbuffer (); For (INT I = 0; I <length; I ++) {result. append (getrandomchar ();} return result. tostring ();}}
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.