Java 生成随机数的 5 种方式,你知道几种? |
您所在的位置:网站首页 › Java的随机数 › Java 生成随机数的 5 种方式,你知道几种? |
原标题:Java 生成随机数的 5 种方式,你知道几种? 1. Math.random 静态方法 产生的随机数是 0 - 1 之间的一个 double ,即 0 commons-lang < artifactId> commons-lang < version> 2.6
API 参考:https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/RandomStringUtils.html 示例: publicclassRandomStringDemo{ publicstaticvoidmain( String[] args) { // Creates a 64 chars length random string of number. String result = RandomStringUtils.random( 64, false, true); System. out.println( "random = "+ result); // Creates a 64 chars length of random alphabetic string. result = RandomStringUtils.randomAlphabetic( 64); System. out.println( "random = "+ result); // Creates a 32 chars length of random ascii string. result = RandomStringUtils.randomAscii( 32); System. out.println( "random = "+ result); // Creates a 32 chars length of string from the defined array of // characters including numeric and alphabetic characters. result = RandomStringUtils.random( 32, 0, 20, true, true, "qw32rfHIJk9iQ8Ud7h0X".toCharArray); System. out.println( "random = "+ result); } } RandomStringUtils 类的实现上也是依赖了 java.util.Random 工具类: RandomStringUtils 类的定义 参考: http://yangzb.iteye.com/blog/325264 http://stackoverflow.com/questions/11051205/difference-between-java-util-random-and-java-security-securerandom - EOF - PS:如果觉得我的分享不错,欢迎大家随手点赞、在看。 大家一起在评论区聊聊呗~返回搜狐,查看更多 责任编辑: |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |