Java---random summary

Source: Internet
Author: User
Tags stringbuffer

 Packagetext;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;ImportJava.util.Random;//Java Random Public classDemo { Public Static voidFirst (string[] args) {Random rand=NewRandom ();  for(inti = 0; I < 5; i++) {            //System.out.println (Math.random ());//Random DecimalsSystem.out.println (Rand.nextint (10));//integer of random 0-9System.out.println (Rand.nextint (10) + 1);//integer of random 1-10System.out.println (Rand.nextint (11) + 10);//integer of random 10-20System.out.println (Rand.nextboolean ());//Random and TrueSystem.out.println (Rand.nextdouble ());//Random Decimals        }    }//Random Date     Public Static voiddate (string[] args) {Random rand=NewRandom (); Calendar C=calendar.getinstance (); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyy-mm-dd HH:mm:ss"); LongStart =c.gettime (). GetTime ();        System.out.println (Sdf.format (C.gettime ())); C.set (2000,1,1,0,0,0); LongEnd =c.gettime (). GetTime ();       System.out.println (Sdf.format (C.gettime ())); //Long time = Math.Round (Rand.nextdouble () * (End-start) + start); //C.settimeinmillis (time);System.out.println (Sdf.format (C.gettime ())); }//randomly take out a letter     Public Static voidWorld (string[] args) {//the difference between string StringBuffer StringBuilderStringBuffer str =NewStringBuffer ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"); StringBuffer Temp=NewStringBuffer (""); Random Rand=NewRandom ();  for(inti = 0; I < 10; i++) {temp.append (Str.charat (Rand.nextint (Str.length ())));    } System.out.println (Temp.tostring ()); }//randomly remove three names and do not repeat Public Static voidMain (string[] args) {string[] arr=Newstring[]{"Li Fei", "Wang Wei", "Guan Yu", "Liu Bei", "Zhang Fei"}; Random Rand=NewRandom (); StringBuffer Temp=NewStringBuffer (""); inti = 3;  while(I > 0) {String name=Arr[rand.nextint (arr.length)]; if(Temp.indexof (name) = =-1) {temp.append (name+" "); I--;    }} System.out.println (Temp.tostring ()); }}

Java---random summary

Related Article

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.