package com.runsky.utils;

import java.util.Random;

public class GetRandom {

    private static final String[] GENERATE_SOURCE = new String[]{"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "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"}; public static String generateByRandom(final int length) {
StringBuilder randomSb = new StringBuilder(length);
Random random = new Random();
for (int i = 0; i < length; i++) {
// 输出字母还是数字
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 字符串
if ("char".equals(charOrNum)) {
// 判断字母大小写
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
randomSb = randomSb.append((char) (choice + random.nextInt(26)));
} else {
randomSb = randomSb.append(random.nextInt(10));
}
}
return randomSb.toString();
} public static void main(String[] args) {
String s = generateByRandom(12);
System.out.println(s);
}
}

【Java】生成随机字符串的更多相关文章

  1. java生成随机字符串

    学习java comparable特性时候,定义如下Student类,需要需要随机添加学生姓名以及学号和成绩,这是java如何随机生成名字,根据我的查询,我找到目前java库支持两种方法. 1. or ...

  2. java 生成随机字符串

    1.生成之指定位数的随机字符串 /** * 随机基数 */ private static char[] charset = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h ...

  3. java生成随机字符串uuid

    GUID是一个128位长的数字,一般用16进制表示.算法的核心思想是结合机器的网卡.当地时间.一个随即数来生成GUID.从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义 ...

  4. .net生成随机字符串

    生成随机字符串的工具类: /// <summary> /// 随机字符串工具类 /// </summary> public class RandomTools { /// &l ...

  5. PHP 生成随机字符串与唯一字符串

    说明:生成随机字符串用到的方法有 mt_rand() 生成唯一字符串用到的方法有 md5(),uniqid(),microtime() 代码: <?php /* * 生成随机字符串 * @par ...

  6. PHP生成随机字符串包括大小写字母

    PHP生成随机字符串包括大小写字母,这里介绍两种方法: 第一种:利用字符串函数操作 <?php /** *@blog <www.phpddt.com> */ function cre ...

  7. java生成随机序列号

    1.java生成随机序列号 String deleteUuid = UUID.randomUUID().toString(); 引用Jar包 //java-uuid-generator-3.1.3.j ...

  8. 生成随机字符串(UUID方法)

    这是另一种用UUID生成随机字符串的方法. public class RandomGenerator{ private int length; public void setLength(int le ...

  9. 【转】Java生成对应字符串的MD5密码模块

    原文网址:http://www.cnblogs.com/xudong-bupt/archive/2013/05/10/3070899.html (1)一般使用的数据库中都会保存用户名和密码,其中密码不 ...

  10. SQL生成随机字符串

    1.SQLserve生成随机字符串 SELECT replace(newid(), '-', '')

随机推荐

  1. arm架构安装mysql5.7

    添加mysql用户组和mysql用户,用于隔离mysql进程 groupadd -r mysql && useradd -r -g mysql -s /sbin/nologin -M ...

  2. java中Atomic变量的实现原理是怎样的?

    转载自: Java3y https://www.zhihu.com/question/39130725/answer/1006948362 一.基础铺垫 首先我们来个例子: public class ...

  3. CCIE DC Multicast Part 4.

    Hi Guys, Part 4 of my CCIE DC multicast article is presented below, in this article we are going to ...

  4. tesseract-ocr 安装、语言库、使用 随记

    前几日才听说ocr的图片识别功能.觉得很有意思.先体验一下. 地址: GitHub - tesseract-ocr/tesseract: Tesseract Open Source OCR Engin ...

  5. pod init 失败

    问题: pod init 失败,报错如下: shenheping@shenhepingdeMacBook-Pro-2 iCar % pod init Ignoring ffi-1.13.1 becau ...

  6. PyQt5模块说明

    pyqt5的类别分为几个模块,包括以下: QtCoreQtGuiQtWidgetsQtMultimediaQtBluetoothQtNetworkQtPositioningEnginioQtWebSo ...

  7. Vue源码中的数据代理

    直接开讲: ​ 由于这个Vue底层封装的函数太多了,我这里只讲思路不说具体的执行了什么函数. ​ const vm=new Vue({这里写一个data,可以是对象也可以是函数}) 在写这段代码的时候 ...

  8. Eclipse 获取maven项目出现问题汇总

    1.errors occurred during the build,可以试试加上下图的配置,路径根据自己jak安装路径去修改

  9. 查看oracle死锁

    select A.sid, b.serial#, decode(A.type, 'MR', 'Media Recovery', 'RT','Redo Thread', 'UN','User Name' ...

  10. Seata安装与使用

    seata版本: 1.6.1 官网 官方文档 下载链接 seata server安装事项 seata server即seata术语中的TC(事务协调者),用于维护全局和分支事务的状态,驱动全局事务提交 ...