国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

史上最全的java隨機數生成算法分享

2019-11-26 15:45:55
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黎川县| 深泽县| 繁峙县| 乌恰县| 万安县| 河间市| 冷水江市| 江源县| 时尚| 兰西县| 尚义县| 德保县| 三江| 黄平县| 大城县| 榕江县| 鄄城县| 达州市| 察雅县| 新巴尔虎左旗| 定安县| 桂阳县| 永寿县| 商洛市| 沈丘县| 新丰县| 淮南市| 崇明县| 大连市| 子长县| 嘉祥县| 东兰县| 浑源县| 连平县| 庄河市| 平舆县| 绿春县| 宝兴县| 麦盖提县| 安丘市| 谷城县|