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