In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to apply Java random numbers". In daily operation, I believe many people have doubts about how to apply Java random numbers. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to apply Java random numbers". Next, please follow the editor to study!
Testing of Java Random numbers
The above usage is illustrated by an example.
Import java.util.Random; / * Java random number test * User: leizhimin * Date: 2008-11-19 17:52:50 * / public class TestRandomNum {public static void main (String [] args) {randomTest (); testNoSeed (); testSeed1 (); testSeed2 () } public static void randomTest () {System.out.println ("- test () -"); / / returns the current time in milliseconds. Long R1 = System.currentTimeMillis (); / / returns the signed double value, which is greater than or equal to 0.0 and less than 1.0. Double R2 = Math.random (); / / get the next random integer int R3 = new Random (). NextInt () through the Random class; System.out.println ("R1 =" + R1); System.out.println ("R2 =" + R2); System.out.println ("R2 =" + R3) } public static void testNoSeed () {System.out.println ("- testNoSeed () -"); / / create an unseeded test Random object Random random = new Random (); for (int I = 0; I
< 3; i++) { System.out.println(random.nextInt()); } } public static void testSeed1() { System.out.println("--------------testSeed1()--------------"); //创建带种子的测试Random对象 Random random = new Random(555L); for (int i = 0; i < 3; i++) { System.out.println(random.nextInt()); } } public static void testSeed2() { System.out.println("--------------testSeed2()--------------"); //创建带种子的测试Random对象 Random random = new Random(); random.setSeed(555L); for (int i = 0; i < 3; i++) { System.out.println(random.nextInt()); } } } 运行结果: --------------test()-------------- r1 = 1227108626582 r3 = 0.5324887850155043 r2 = -368083737 --------------testNoSeed()-------------- 809503475 1585541532 -645134204 --------------testSeed1()-------------- -1367481220 292886146 -1462441651 --------------testSeed2()-------------- -1367481220 292886146 -1462441651 Process finished with exit code 0 通过testSeed1()与testSeed2()方法的结果可以看到,两个打印结果相同,因为他们种子相同,再运行一次,结果还是一样的,这就是带种子随机数的特性。而不带种子的,每次运行结果都是随机的。 Java随机数的综合应用 下面通过最近写的一个随机数工具类来展示用法: import java.util.Random; /** * 随机数、随即字符串工具 * User: leizhimin * Date: 2008-11-19 9:43:09 */ public class RandomUtils { public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static final String letterChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 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 ("abnormal conversion of a number + num +" to a string of length "+ fixdlenth +");} sb.append (strNum); return sb.toString () } / * generate a fixed length string based on the number, which is not long enough to complement 0 * * @ param num number * @ param fixdlenth string length * @ return fixed length string * / public static String toFixdLengthString (int 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 ("abnormal conversion of a number + num + into a string of length" + fixdlenth + ");} sb.append (strNum); return sb.toString ();} public static void main (String [] args) {System.out.println (generateString (15)) System.out.println (generateMixString (15)); System.out.println (generateLowerString (15)); System.out.println (generateUpperString (15)); System.out.println (generateZeroString (15)); System.out.println (toFixdLengthString (123,15)); System.out.println (toFixdLengthString (123L, 15));}}
Running result:
VWMBPiNbzfGCpHG 23hyraHdJkKPwMv tigowetbwkm1nde BPZ1KNEJPHB115N 000000000000000000000000123000000000000123 Process finished with exit code 0 this is the end of the study on "how to apply Java random numbers", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.