NumberFormat format=NumberFormat.getInstance();
format.setMinimumIntegerDigits(6);
System.out.println(format((int)(Math.random()*1000000)));

解决方案 »

  1.   


    import java.util.*;
    import java.security.SecureRandom;
    import java.security.NoSuchAlgorithmException;
    import java.security.NoSuchProviderException;public class RandomNum
    {
    static Long randomnum = null;
    static Float randomfloat = null;
    static boolean floatvalue = false;
    static long upper = 100;
    static long lower = 0;
    static String algorithm = null;
    static String provider = null;
    static boolean secure = false;
    static Random random = null;
    static SecureRandom secrandom = null; private static float getFloat()
    {
    if (random == null)
    {
    return secrandom.nextFloat();
    }
    else
    {
    return random.nextFloat();
    }
    } public static void generateRandomObject() throws Exception
    {
    // check to see if the object is a SecureRandom object
    if (secure)
    {
    try
    {
    // get an instance of a SecureRandom object
    if (provider != null)
    // search for algorithm in package provider
    secrandom = SecureRandom.getInstance(algorithm, provider);
    else
    secrandom = SecureRandom.getInstance(algorithm);
    }
    catch (NoSuchAlgorithmException ne)
    {
    throw new Exception(ne.getMessage());
    }
    catch (NoSuchProviderException pe)
    {
    throw new Exception(pe.getMessage());
    }
    }
    else
    {
    random = new Random();
    }
    } /**
    * generate the random number
    */
    private static void generaterandom()
    {
    int tmprandom = 0; // temp storage for random generated number
    Integer rand; // check to see if float value is expected
    if (floatvalue)
    {
    randomfloat = new Float(getFloat());
    }
    else
    {
    randomnum = new Long(lower + (long) ((getFloat() * (upper - lower))));
    }
    } public static Number getRandom()
    {
    generaterandom();
    if (floatvalue)
    return randomfloat;
    else
    return randomnum;
    } public static void setRange(long low, long up)
    {
    // set the upper and lower bound of the range
    lower = low;
    upper = up; // check to see if a float value is expected
    if ((lower == 0) && (upper == 1))
    floatvalue = true;
    } /**
    * set the algorithm name
    *
    * @param value name of the algorithm to use for a SecureRandom object
    *
    */
    public static void setAlgorithm(String value)
    {
    algorithm = value;
    secure = true; // a SecureRandom object is to be used
    } public static void setProvider(String value)
    {
    provider = value;
    } public static void setRange(String value) throws Exception
    {
    try
    {
    upper = new Integer(value.substring(value.indexOf('-') + 1)).longValue();
    }
    catch (Exception ex)
    {
    throw new Exception("upper attribute could not be" +
    " turned into an Integer default value was used");
    } try
    {
    lower = new Integer(value.substring(0, value.indexOf('-'))).longValue();
    }
    catch (Exception ex)
    {
    throw new Exception("lower attribute could not be" +
    " turned into an Integer default value was used");
    } if ((lower == 0) && (upper == 1))
    {
    floatvalue = true;
    } if (upper < lower)
    {
    throw new Exception("You can't have a range where the lowerbound" +
    " is higher than the upperbound.");
    }
    }
    }
      

  2.   

    上帖有点笔误
    更新
    NumberFormat format=NumberFormat.getInstance();
    format.setGroupingUsed(false);
    format.setMinimumIntegerDigits(6);
    System.out.println(format.format((int)(Math.random()*1000000)));
      

  3.   

    public static String genPwd(int size) {  
            String all="0123456789";
            char[] result = new char[size];
            for(int i=0;i<size;i++) {
                result[i]=all.charAt((int)(Math.random()*all.length()));
            }
            return new String(result);
    }
      

  4.   

    这么简单的问题,怎么好问阿,没有动脑子阿long xxx,yyy,zzz;
    xxx=rand();
    yyy=rand()+1000000;
    zzz=(xxx+yyy)%1000000;  //先随便弄到一个大于6位的数字,然后对他求7为数字余数九可以阿。这么简单,你什么学校毕业阿
    那位阿妹居然弄出格类来解决这个问题,可笑的!
      

  5.   

    public static String getPassword() {
    Random random = new Random();
    String password = "";
    while (random.nextInt(10) != -1) {
    for (int i = 0; i < 6; i++) {
    password = password + random.nextInt(10);
    if (i == 5) {
    break;
    }
    }
    if (password.length() == 6) {
    break;
    }
    }
    return password;
    }
      

  6.   

    回复人: yangyouyi(yangyouyi) ( ) 你的办法不好用的如果是 100 ,就是三位数了。