import java.text.*;
import java.util.*;public class RandomNum {
  public String RandomNum () throws Exception {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "HHMMmmddss" );
    StringBuffer time = new StringBuffer( simpleDateFormat.format( Calendar.
getInstance().
getTime() ) );
    Random rand = new Random();
    for ( int i = 0; i < 5; i++ ) {
      time.append( rand.nextInt( 9 ) ); //每次生成一位的随机数并加再str后面.这样不用考虑高位为0的情况
    }
    return time.toString();
  }
  
  public void genRandomNum(int count)throws Exception{
      for(int i=1;i<=count;i++){
          String s = RandomNum();
          System.out.println(s);
      }
  }
  
  public static void main(String[] args)throws Exception{
      RandomNum rn = new RandomNum();
      rn.genRandomNum(10);
  }
}

解决方案 »

  1.   

    import java.text.*;
    import java.util.*;public class RandomNum {
      public String RandomNum () throws Exception {    Random rand = new Random();//提前到这里就好了    SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "HHMMmmddss" );
        StringBuffer time = new StringBuffer( simpleDateFormat.format( Calendar.
    getInstance().
    getTime() ) );
        
        for ( int i = 0; i < 5; i++ ) {
          time.append( rand.nextInt( 9 ) ); //每次生成一位的随机数并加再str后面.这样不用考虑高位为0的情况
        }
        return time.toString();
      }
      
      public void genRandomNum(int count)throws Exception{
          for(int i=1;i<=count;i++){
              String s = RandomNum();
              System.out.println(s);
          }
      }
      
      public static void main(String[] args)throws Exception{
          RandomNum rn = new RandomNum();
          rn.genRandomNum(10);
      }
    }