当前页面: 开发资料首页 → J2SE 专题 → 如何产生20-9999之间的随机数
如何产生20-9999之间的随机数
摘要: 如何产生20-9999之间的随机数
如何产生20-9999之间的随机数
import java.util.Random;
/**
* @author mengfanpp
*
*/
public class SuiJiShu {
private static int getRandom() {
Random random = new Random();
int rValue = Math.abs(random.nextInt()) % 10000; // 保证在0-9999之间
if (rValue < 20) {
rValue = Math.abs(random.nextInt()) % 10000;
}
return rValue;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++) {
System.out.println(getRandom());
}
}
}
public class Test
{
public static void main(String[] args)
{
for ( int i = 0; i<100; i++ )
{
int random = (int)(Math.random()*9999);
if ( random > 20 )
System.out.println(random);
}
}
}
public class Test
{
public static void main(String[] args)
{
int random = 20 + (int)(Math.random()*9980);
System.out.println(random);
}
}
这样做就可以了
楼上正解!
JF
樓上的方法似乎好使.....
支持一下。
高手的代码果然简洁,顶
项一下吧
佩服dzhxzh!