史上最全的java随机数生成算法分享(转)

这篇文章主要介绍了史上最全的java随机数生成算法,我分享一个最全的随机数的生成算法,最代码的找回密码的随机数就是用的这个方法

1 String password = RandomUtil.generateString(10);

源码如下:

  1 package com.javaniu.core.util;
  2 import java.util.Random;
  3 public class RandomUtil {
  4  public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  5  public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  6  public static final String NUMBERCHAR = "0123456789";
  7  /**
  8   * 返回一个定长的随机字符串(只包含大小写字母、数字)
  9   * 
 10   * @param length
 11   *            随机字符串长度
 12   * @return 随机字符串
 13   */
 14  public static String generateString(int length) {
 15   StringBuffer sb = new StringBuffer();
 16   Random random = new Random();
 17   for (int i = 0; i < length; i++) {
 18    sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
 19   }
 20   return sb.toString();
 21  }
 22  /**
 23   * 返回一个定长的随机纯字母字符串(只包含大小写字母)
 24   * 
 25   * @param length
 26   *            随机字符串长度
 27   * @return 随机字符串
 28   */
 29  public static String generateMixString(int length) {
 30   StringBuffer sb = new StringBuffer();
 31   Random random = new Random();
 32   for (int i = 0; i < length; i++) {
 33    sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));
 34   }
 35   return sb.toString();
 36  }
 37  /**
 38   * 返回一个定长的随机纯大写字母字符串(只包含大小写字母)
 39   * 
 40   * @param length
 41   *            随机字符串长度
 42   * @return 随机字符串
 43   */
 44  public static String generateLowerString(int length) {
 45   return generateMixString(length).toLowerCase();
 46  }
 47  /**
 48   * 返回一个定长的随机纯小写字母字符串(只包含大小写字母)
 49   * 
 50   * @param length
 51   *            随机字符串长度
 52   * @return 随机字符串
 53   */
 54  public static String generateUpperString(int length) {
 55   return generateMixString(length).toUpperCase();
 56  }
 57  /**
 58   * 生成一个定长的纯0字符串
 59   * 
 60   * @param length
 61   *            字符串长度
 62   * @return 纯0字符串
 63   */
 64  public static String generateZeroString(int length) {
 65   StringBuffer sb = new StringBuffer();
 66   for (int i = 0; i < length; i++) {
 67    sb.append('0');
 68   }
 69   return sb.toString();
 70  }
 71  /**
 72   * 根据数字生成一个定长的字符串,长度不够前面补0
 73   * 
 74   * @param num
 75   *            数字
 76   * @param fixdlenth
 77   *            字符串长度
 78   * @return 定长的字符串
 79   */
 80  public static String toFixdLengthString(long num, int fixdlenth) {
 81   StringBuffer sb = new StringBuffer();
 82   String strNum = String.valueOf(num);
 83   if (fixdlenth - strNum.length() >= 0) {
 84    sb.append(generateZeroString(fixdlenth - strNum.length()));
 85   } else {
 86    throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
 87      + "的字符串发生异常!");
 88   }
 89   sb.append(strNum);
 90   return sb.toString();
 91  }
 92  /**
 93   * 每次生成的len位数都不相同
 94   * 
 95   * @param param
 96   * @return 定长的数字
 97   */
 98  public static int getNotSimple(int[] param, int len) {
 99   Random rand = new Random();
100   for (int i = param.length; i > 1; i--) {
101    int index = rand.nextInt(i);
102    int tmp = param[index];
103    param[index] = param[i - 1];
104    param[i - 1] = tmp;
105   }
106   int result = 0;
107   for (int i = 0; i < len; i++) {
108    result = result * 10 + param[i];
109   }
110   return result;
111  }
112  public static void main(String[] args) {
113   System.out.println("返回一个定长的随机字符串(只包含大小写字母、数字):" + generateString(10));
114   System.out
115     .println("返回一个定长的随机纯字母字符串(只包含大小写字母):" + generateMixString(10));
116   System.out.println("返回一个定长的随机纯大写字母字符串(只包含大小写字母):"
117     + generateLowerString(10));
118   System.out.println("返回一个定长的随机纯小写字母字符串(只包含大小写字母):"
119     + generateUpperString(10));
120   System.out.println("生成一个定长的纯0字符串:" + generateZeroString(10));
121   System.out.println("根据数字生成一个定长的字符串,长度不够前面补0:"
122     + toFixdLengthString(123, 10));
123   int[] in = { 1, 2, 3, 4, 5, 6, 7 };
124   System.out.println("每次生成的len位数都不相同:" + getNotSimple(in, 3));
125  }
126 }

有图有真相:

java随机字符补充版 

今天在zuidaimai看到一个java随机字符生成demo,正好要用,但发现不完整,重新整理一下,分享给有需要的朋友

 1 public static void main(String[] args) {    
 2         //String s = RandomNum.getRandomNumStr(5);
 3         //System.out.println(s);
 4         System.out.println("生成5个含有5个字符的字符串:");
 5         RandomNum.SuiJiZiFuChuan(5,5);
 6         System.out.println("生成3个含有6个字符的字符串:");
 7         RandomNum.SuiJiZiFuChuan(6,3);
 8         System.out.println("生成任意1到20个含有任意1到10个字符的字符串:");
 9         RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),(int)(10*Math.random()));
10         System.out.println("随机性生成字符:");
11         int i=0;
12         while(i<(int)(10*Math.random())){
13             RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),1);
14             i++;
15         }
16     }   
17     public static void SuiJiZiFuChuan(int x,int y){
18         for(int j=0;j<y;j++){
19             for(int i=0;i<x;i++){
20                 int a=(int)(100*Math.random()+100*Math.random());
21                 while(true){
22                     if(a>96&a<123)
23                         break;
24                     else
25                         a=(int)(100*Math.random()+100*Math.random());
26                 }
27                 System.out.print((char)a);
28             }
29             System.out.println();
30         }
31     }

执行结果:



posted @ 2016-12-22 14:57  海天依色  阅读(2442)  评论(0编辑  收藏  举报