数_机_随

http://liuyuru.iteye.com/blog/777371 随机数
String str = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0123456789"; //$NON-NLS-1$
        Random random = new Random();
        String result = "";
        for (int i = 0; i < 4; i++)
        {
            result = result.concat(String.valueOf(str.charAt(random.nextInt(str.length()))));
        }




       
        System.out.println("任意一个2到32之间的偶数:" + a(2, 32));
       
        System.out.println("任意小写字符:" + b('a', 'z'));
       
        System.out.println("任意大写字符:" + b('A', 'Z'));
       
        System.out.println("0-9任意字符:" + b('0', '9'));
       
        c();
   
    public static int a(double num1, double num2)
    {
        int s = (int)num1 + (int)(Math.random() * (num2 - num1));
        if (s % 2 == 0)
        {
            return s;
        }
        else
        {
            return s + 1;
        }
    }
   
    public static char b(char a, char b)
    {
        return (char)(a + Math.random() * (b - a + 1));
    }
   
    public static void c()
    {
        Random r = new Random();
        System.out.println("随机产生一个整数:" + r.nextInt());
        System.out.println("随机产生一个大于0,小于10的整数:" + r.nextInt(10));
        System.out.println("随机产生一个boolean值:" + r.nextBoolean());
        System.out.println("随机产生一个双精度型的值:" + r.nextDouble());
        System.out.println("随机产生一个浮点型:" + r.nextFloat());
        System.out.println("随机产生一个概率密度为高斯分布的双精度值:" + r.nextGaussian());
    }
}

//生成一个4位包含大小写,数字的随机数
        String str = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 4; i++)
        {
            sb = sb.append(str.charAt(random.nextInt(62)));
        }
        System.out.println(sb.toString());

你可能感兴趣的:(Blog)