java程序中怎样生成0到9的6个随机数,谢谢啦!要完整的程序,谢谢~~

如题所述

第1个回答  推荐于2016-05-09
public class MyRandom {
static Random r = new Random();
static String ssource = "0123456789";
static char[] src = ssource.toCharArray();
//产生随机字符串
private static String randString (int length)
{
char[] buf = new char[length];
int rnd;
for(int i=0;i<length;i++)
{
rnd = Math.abs(r.nextInt()) % src.length;

buf[i] = src[rnd];
}
return new String(buf);
}

//调用该方法,产生随机字符串,
//参数i: 为字符串的长度
public static String runVerifyCode(int i)
{
String VerifyCode = randString(i);
return VerifyCode;
}

public static void main(String[] args) {
MyRandom t=new MyRandom();
t.runVerifyCode(10);
}
}
在生成随机数的地方直接调用上面的 MyRandom.runVerifyCode(int i)
;i是你需要生成几位随机数,追问

谢谢啦!

追答

至于说将随机数放到数组中,以后可以直接取,那就不叫随机数了。

本回答被提问者采纳
第2个回答  2011-04-25
public static void main(String [] args)
{
int temp[]=new int[6];
for(int i=0;i<6;i++)
{
temp[i]=(int)(Math.random()*10);
}
for(int i=0;i<6;i++)
{
System.out.println(temp[i]);
}

}
随机数我存入数组了,以后使用的时候直接调用数据就行追问

很方便,谢谢~~!

第3个回答  2011-04-25
Random rd1 = new Random();
// 按均匀分布产生长整数
String key = rd1.nextInt(9) + "" + rd1.nextInt(9) + "" + rd1.nextInt(9)+""+ rd1.nextInt(9) + ""+ rd1.nextInt(9) + "" + rd1.nextInt(9) + "";追问

事实上,俺还不会,t题目是这样的,编写一个JSP程序,在首行用4号字居中显示“欢迎来到JSP实验室,您是第*位访客!”;之后建立一个随机数发生器,在0-9之间随机生成6个数。页面结果如下:

您看看怎么写才好

追答

页面效果未看到!!!

追问

效果是
欢迎来到JSP实验室,您是第1位访客!
这里产生了6个0~9之间的随机机数8 6 7 6 6 3