Randomly generated 5-bit case letters or digital __java base

Source: Internet
Author: User
Tags rand

randomly generated 5-bit uppercase or lowercase letters or numbers

Method One: Generate a duplicate

public static void Main (string[] args) {
		Random rand = new Random ();
		char[] letters=new char[]{' 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 ', ' 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 ', ' r ', '
				0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
		String str = "";
		int index;
		Boolean[] flags = new boolean[letters.length];//defaults to False for
		(int i=0;i<5;i++) {
			do{
				index = Rand.nextint (letters.length); 
			} while (flags[index]==true);
			char C = letters[index];
			STR + C;
			flags[index]=true;
		}
		System.out.println (str);
	

Method Two: Generate duplicates, similar to method one

public static void Main (string[] args) {
		Random rand = new Random ();
		char[] letters=new char[]{' 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 ', ' 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 ', ' r ', '
				0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
		String str = "";
		int index;
		Boolean[] flags = new boolean[letters.length];//defaults to False for
		(int i=0;i<5;i++) {
			do{
				index = Rand.nextint (letters.length); 
			} while (flags[index]==true);
			char C = letters[index];
			STR + C;
			flags[index]=true;
		}
		System.out.println (str);
	
Method Three: Generate Duplicates (recommended option)

public static void Main (string[] args) {
		String str = "";
		Random rand = new Random ();
		for (int i=0;i<5;i++) {
			int num = rand.nextint (3);
			Switch (num) {case
				0:
					char c1 = (char) (Rand.nextint + ' a ');//Generate random lowercase letter 
					str + c1;
					break;
				Case 1:
					char c2 = (char) (rand.nextint () + ' A ');//Generate Random uppercase letters 
					STR + c2;
					break;
				Case 2:
					str = rand.nextint (10);//Generate Random
			number
		}
		System.out.println ("generated 5 random Captcha is:" +str);
	}




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.