博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java生成随机数
阅读量:6934 次
发布时间:2019-06-27

本文共 1218 字,大约阅读时间需要 4 分钟。

生成不定长度的随机字符串:

public class RandomUtils {    public static final String allChar="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";    public static String generateString(){        StringBuffer sb = new StringBuffer();        Random random = new Random();        int x = random.nextInt(47);        for (int i = 0; i < x; i++) {            sb.append(allChar.charAt(random.nextInt(allChar.length())));        }        return sb.toString();    }        //test code    public static void main(String[] args) {        for (int i = 0; i < 20; i++) {            System.out.println(generateString());        }            }}

生成指定长度的字符串:

public static final String randomString(int length) {          Random randGen = null;          char[] numbersAndLetters = null;            if (length < 1) {                return null;            }            if (randGen == null) {                   randGen = new Random();                   numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +                      "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();                    }            char [] randBuffer = new char[length];            for (int i=0; i

 

转载于:https://www.cnblogs.com/hutton/p/4299146.html

你可能感兴趣的文章
一朵为员工赋能的“美”云
查看>>
PostgreSQL Oracle 兼容性之 - PL/SQL DETERMINISTIC 与PG函数稳定性(immutable, stable, volatile)...
查看>>
万万想不到,你是这样的“闲鱼”!
查看>>
Logstash 推送告警到阿里钉钉(Dingtalk)
查看>>
软银机器人Pepper上岗必胜客,顾客可通过机器人预订披萨
查看>>
较主流的消息队列的比较与选型
查看>>
SQL SERVER全面优化-------写出好语句是习惯
查看>>
安卓 AsyncHttpClient - “Content-Type not allowed!”
查看>>
samba
查看>>
虚拟机克隆步骤
查看>>
ListView使用技巧
查看>>
MySQL共享存储主备模式利用Keepalived实现双机高可用
查看>>
作为AI的“辅助大臣”,区块链的前途不可限量
查看>>
学习笔记:vsphere6 迁移物理机,指定被迁移的IP报错
查看>>
都说做ToB商业模式,VR AR博物馆内容的矿或许可以挖下
查看>>
JQuery用户名无刷新验证
查看>>
ubuntu安装和查看已安装
查看>>
[Linux]Shell的运算符和特殊变量
查看>>
c++ ado 程序终止时崩溃
查看>>
关于行号输出的简单命令
查看>>