JAVA 生存 随即6 位 字母+数字

来源:互联网 发布:stm32编程实例 编辑:程序博客网 时间:2024/06/06 17:19
Java代码  收藏代码
  1. /** 
  2.  * 0-9 ASC 48-57 
  3.  * A-Z ASC 65-90 
  4.  * a-z ASC 97-122 
  5.  */  
  6. package com.nutsbling.rand;  
  7.   
  8. import java.util.Random;  
  9.   
  10. public class RandNumCharacter {  
  11.       
  12.     /** 
  13.      *  
  14.      * @param begin 区间段的最小集合 
  15.      * @param end 区间段的最大集合 
  16.      * @return 
  17.      */  
  18.     public static int iRandom(int []begin, int []end){  
  19.         Random r =new Random();  
  20.         int index = r.nextInt(begin.length);   
  21.         int size = end[index]-begin[index];  
  22.         return r.nextInt(size)+begin[index]; //产生随机数后的ASC  
  23.           
  24.     }  
  25.   
  26.     /** 
  27.      * @param args 
  28.      */  
  29.     public static void main(String[] args) {  
  30.         // TODO Auto-generated method stub  
  31.         int []begin={48,65,97};  
  32.         int []end={57,90,122};  
  33.         char []c = new char[6];  
  34.         for(int i=0;i<6;i++){  
  35.             c[i]=(char) iRandom(begin, end);  
  36.           
  37.         }  
  38.         System.out.println(new String(c));  
  39.     }  
  40.   
  41. }  


原创粉丝点击