Random number Test

Source: Internet
Author: User
Tags time in milliseconds

Learning Links http://lavasoft.blog.51cto.com/62575/113758/


Import Java.util.Random; /** * Random number, then string tool * User:leizhimin * date:2008-11-19 9:43:09 */public class Randomutils {public static final S Tring Allchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static final String Letterchar = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static final String Numberchar = "0123456789"; /** * Returns a fixed-length random string (containing only uppercase and lowercase letters, numbers) * * @param length random string lengths * @return Random String */ 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 (); }/** * Returns a fixed-length random, pure-letter string (containing only uppercase and lowercase letters) * * @param length random string lengths * @returnRandom String */public static string generatemixstring (int length) {stringbuffer sb = new Strin Gbuffer (); Random random = new random (); for (int i = 0; i < length; i++) {Sb.append (Allchar.charat (Random.nextint (Letterchar.length ()) )); } return sb.tostring (); }/** * Returns a fixed-length random pure capital letter string (with uppercase and lowercase letters only) * * @param length random string lengths * @return Random string */public static String generatelowerstring (int length) {return generatemixstring (length ). toLowerCase (); }/** * Returns a fixed-length random plain lowercase letter string (with only uppercase letters) * * @param length random string lengths * @return Random string */public static String generateupperstring (int length) {return generatemixstring (length ). toUpperCase (); }/** * Generates a fixed-length pure 0 String * * @param length stringLength * @return Pure 0 String */public static string generatezerostring (int length) {Stri Ngbuffer sb = new StringBuffer (); for (int i = 0; i < length; i++) {sb.append (' 0 '); } return sb.tostring (); /** * Generates a fixed-length string based on the number, length is not enough for the front 0 * * @param num number * @param fixdlen Th string length * @return fixed-length string */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 ("Convert number + num +" to a string with a length of "+ Fixdlenth +") An exception occurred! "); } sb.append (Strnum); return sb.tostring (); /** * Generates a fixed-length string based on the number, length is not enough for the front 0 * * @param num number * @param fixdlen Th 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 ("Convert number + num +" to a string with a length of "+ Fixdlenth +") An exception occurred! "); } 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)); } }

Small random number experiment:

Import Java.util.Random; /** * java random number Test * user:leizhimin * date:2008-11-19 17:52:50 * * public class Testrandomnum {public static void M                 Ain (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 a double value with a positive sign, greater than or equal to 0.0, less than 1.0.                 Double r2 = math.random ();                 By using the random class to get the next random integer int r3 = new Random (). Nextint ();                 System.out.println ("r1 =" + r1);                 SYSTEM.OUT.PRINTLN ("r3 =" + R2);         SYSTEM.OUT.PRINTLN ("r2 =" + R3); public static void Testnoseed () {System.out.println ("--------------testnoseed ()--------------")                 ; To create a test random object without a seed                Random random = new random ();                 for (int i = 0; i < 3; i++) {System.out.println (Random.nextint ()); }} public static void TestSeed1 () {System.out.println ("--------------testSeed1 ()--------                 ------");                 Create a test random object with a seed 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 ()--------                 ------");                 Create a test random object with a seed random random = new random ();                 Random.setseed (555L);                 for (int i = 0; i < 3; i++) {System.out.println (Random.nextint ()); }         } }

  

Random number Test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.