java 个人学习碰到的一些关于随机数的问题
整合在此供大家参考
/** * 生成随机大小写字母 * @author: lyq * @date: May 16, 2014 3:26:13 PM */ public void Test1(){ String str=""; for(int i=0;i<3;i++){//你想生成几个字符的,就把3改成几,如果改成1,那就生成一个随机字母. // str= str+(char) (Math.random ()*26+'a'); //生成随机小写字母 str= str+(char) (Math.random ()*26+'A'); } System.out.println(str); }
/** * 生成随机整数 * @author: lyq * @date: May 16, 2014 3:27:40 PM */ public void Test2(){ Random random = new Random(); int i = random.nextInt(); //int范围类的随机数,看你定义什么范围的 i = random.nextInt(10); //生成0-10以内的随机数 , 10 可以随意定义 只要不超过int范围就行 System.out.println(i+"random"); i = (int)(Math.random() * 10); //0-10以内的随机数,用Matn.random()方式 System.out.println(i+"Math.random"); }