Java random number random disrupt random array Extraction

Source: Internet
Author: User

Recently, I encountered some random numbers, randomly disrupted arrays, and other random processing problems in my project. Let's make a summary today. 1. the following code generates a random password with a specified length: [java] import java. util. random; public class test {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub System. out. println (genRandomNum (10 ));} /*** generate the password immediately ** @ param pwd_len * total length of the generated password * @ return password String */public static String genRandomNum (int pwd_len) {// 35 is because the array starts from 0 and 26 letters + 10 digits final int maxNum = 36; int I; // the random number int c generated Ount = 0; // The length of the generated password char [] str = {'A', 'B', 'C', 'D', 'E ', 'F', 'G', 'h', 'I', 'J', 'k', 'l', 'M', 'n', 'O ', 'P', 'Q', 'R','s ', 't', 'U', 'V', 'w', 'x', 'y ', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8 ', '9'}; StringBuffer pwd = new StringBuffer (""); Random r = new Random (); while (count <pwd_len) {// generate a Random number, take the absolute value, prevents negative numbers. I = Math. abs (r. nextInt (maxNum); // The maximum number of generated data is 36-1 if (I> = 0 & I <st R. length) {pwd. append (str [I]); count ++ ;}} return pwd. toString () ;}} 2. disrupt the order of an array and generate a random array. here we need to use the shuffle () method in the java tool Collections. The Code is as follows: [java]/*** immediately breaks down an ordered array */import java. util. arrayList; import java. util. collections; import java. util. iterator; import java. util. list; public class Shuffle {public static void main (String [] args) {shuffle ();} public static void shuffle () {int [] x = {1, 2, 3, 4, 5, 6, 7, 8, 9}; List list = new ArrayList (); for (int I = 0; I <x. length; I ++) {System. out. print (x [I] + ","); list. add (x [I]);} System. out. pr Intln (); Collections. shuffle (list); Iterator ite = list. iterator (); while (ite. hasNext () {System. out. print (ite. next (). toString () + ",") ;}} 3. randomly retrieve the specified n number from the array. The Code is as follows: [java]/*** Description: Randomly retrieve the specified n number from the array. * @ Param ArrayList original array * @ param int n number of random extracts * @ return returns the extracted array */public static ArrayList getRandomArray (ArrayList list, int num) {ArrayList aList = new ArrayList (); for (int I = 0; I <num; I ++) {int randomNum = getRandom (list. size (); aList. add (list. get (randomNum); list. remove (randomNum);} return aList}. Write so much first, and then update it if there is a "random" problem.

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.