站内搜索: 请输入搜索关键词

当前页面: 开发资料首页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!


↑返回目录
前一篇: 打包jar问题
后一篇: 写一个delString(String s1,String s2)