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

所属分类: 软件编程 / java 阅读数: 66
收藏 0 赞 0 分享

复制代码 代码如下:

String password = RandomUtil.generateString(10);

源码如下:

复制代码 代码如下:

package com.javaniu.core.util;
import java.util.Random;
public class RandomUtil {
 public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 public static final String NUMBERCHAR = "0123456789";

 /**
  * 返回一个定长的随机字符串(只包含大小写字母、数字)
  *
  * @param length
  *            随机字符串长度
  * @return 随机字符串
  */
 public static String generateString(int length) {
  StringBuffer sb = new StringBuffer();
  Random random = new Random();
  for (int i = 0; i < length; i++) {
   sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
  }
  return sb.toString();
 }

 /**
  * 返回一个定长的随机纯字母字符串(只包含大小写字母)
  *
  * @param length
  *            随机字符串长度
  * @return 随机字符串
  */
 public static String generateMixString(int length) {
  StringBuffer sb = new StringBuffer();
  Random random = new Random();
  for (int i = 0; i < length; i++) {
   sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));
  }
  return sb.toString();
 }

 /**
  * 返回一个定长的随机纯大写字母字符串(只包含大小写字母)
  *
  * @param length
  *            随机字符串长度
  * @return 随机字符串
  */
 public static String generateLowerString(int length) {
  return generateMixString(length).toLowerCase();
 }

 /**
  * 返回一个定长的随机纯小写字母字符串(只包含大小写字母)
  *
  * @param length
  *            随机字符串长度
  * @return 随机字符串
  */
 public static String generateUpperString(int length) {
  return generateMixString(length).toUpperCase();
 }

 /**
  * 生成一个定长的纯0字符串
  *
  * @param length
  *            字符串长度
  * @return 纯0字符串
  */
 public static String generateZeroString(int length) {
  StringBuffer sb = new StringBuffer();
  for (int i = 0; i < length; i++) {
   sb.append('0');
  }
  return sb.toString();
 }

 /**
  * 根据数字生成一个定长的字符串,长度不够前面补0
  *
  * @param num
  *            数字
  * @param fixdlenth
  *            字符串长度
  * @return 定长的字符串
  */
 public static String toFixdLengthString(long num, int fixdlenth) {
  StringBuffer sb = new StringBuffer();
  String strNum = String.valueOf(num);
  if (fixdlenth - strNum.length() >= 0) {
   sb.append(generateZeroString(fixdlenth - strNum.length()));
  } else {
   throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
     + "的字符串发生异常!");
  }
  sb.append(strNum);
  return sb.toString();
 }

 /**
  * 每次生成的len位数都不相同
  *
  * @param param
  * @return 定长的数字
  */
 public static int getNotSimple(int[] param, int len) {
  Random rand = new Random();
  for (int i = param.length; i > 1; i--) {
   int index = rand.nextInt(i);
   int tmp = param[index];
   param[index] = param[i - 1];
   param[i - 1] = tmp;
  }
  int result = 0;
  for (int i = 0; i < len; i++) {
   result = result * 10 + param[i];
  }
  return result;
 }

 public static void main(String[] args) {
  System.out.println("返回一个定长的随机字符串(只包含大小写字母、数字):" + generateString(10));
  System.out
    .println("返回一个定长的随机纯字母字符串(只包含大小写字母):" + generateMixString(10));
  System.out.println("返回一个定长的随机纯大写字母字符串(只包含大小写字母):"
    + generateLowerString(10));
  System.out.println("返回一个定长的随机纯小写字母字符串(只包含大小写字母):"
    + generateUpperString(10));
  System.out.println("生成一个定长的纯0字符串:" + generateZeroString(10));
  System.out.println("根据数字生成一个定长的字符串,长度不够前面补0:"
    + toFixdLengthString(123, 10));
  int[] in = { 1, 2, 3, 4, 5, 6, 7 };
  System.out.println("每次生成的len位数都不相同:" + getNotSimple(in, 3));
 }
}


有图有真相:

java随机字符补充版

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

复制代码 代码如下:

public static void main(String[] args) {   
        //String s = RandomNum.getRandomNumStr(5);
        //System.out.println(s);
        System.out.println("生成5个含有5个字符的字符串:");
        RandomNum.SuiJiZiFuChuan(5,5);
        System.out.println("生成3个含有6个字符的字符串:");
        RandomNum.SuiJiZiFuChuan(6,3);
        System.out.println("生成任意1到20个含有任意1到10个字符的字符串:");
        RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),(int)(10*Math.random()));
        System.out.println("随机性生成字符:");
        int i=0;
        while(i<(int)(10*Math.random())){
            RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),1);
            i++;
        }
    }   

    public static void SuiJiZiFuChuan(int x,int y){
        for(int j=0;j<y;j++){
            for(int i=0;i<x;i++){
                int a=(int)(100*Math.random()+100*Math.random());
                while(true){
                    if(a>96&a<123)
                        break;
                    else
                        a=(int)(100*Math.random()+100*Math.random());
                }
                System.out.print((char)a);
            }
            System.out.println();
        }
    }

执行结果:



来源: http://www.zuidaima.com/share/1585762703215616.htm

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多