public class RadomTest {
public static void main(String []str){
double d = Math.random()*67;
while(d<33 || d>66){
d = Math.random()*67;
}
System.out.println("number"+d);
}
}
不知道这样行不行.可能得到的那个字还要format 一下.
public static void main(String []str){
double d = Math.random()*67;
while(d<33 || d>66){
d = Math.random()*67;
}
System.out.println("number"+d);
}
}
不知道这样行不行.可能得到的那个字还要format 一下.
{
public static void main(String[] args)
{ int a=33;
int b=0;
double c=33.0*Math.random();
b=(int)(a+c);
System.out.println(b);
}
}
{
public static void main(String[] args)
{
int a=0;
double b=33.0*Math.random();
int c=0;
a=(int)(33+b);
while(b!=38&b!=44)
{
c=a;
System.out.println(c);
}
}
}
c就是你要的数
你是在33& 66之间取的时候不能有重复的数出现.
是不是只取两个,那只要加一个判断条件就OK了,如果是取33与66之间的33个不重复的数,那这样的话,你可能要用到hashmap实现了.
public class RandomInt
{
public static void main(String[] args)
{
int a=0;
double b=33.0*Math.random();
int c=0;
while(b!=38&b!=44)
{
b=33.0*Math.random();
a=(int)(33+b);
c=a;
System.out.println(c);
break;
}
}
}
{
public static void main(String[] args)
{
int i;
for (int j = 0; j < 30; ++j)
{
i = (int) (33 + Math.random() * 33);
System.out.println(i);
}
}
}
i = (int) (33 + Math.random() * 34);其实可以做一个通用的得到某区间随机数的方法,以方便使用,比如: static int randomNew(int begin,int end)
{
return (int) (begin + Math.random() * (end-begin+1));
}
以下是得到从66到99之间的随机数的例子
public class RandomNumber
{
static int randomNew(int begin,int end)
{
return (int) (begin + Math.random() * (end-begin+1));
}
public static void main(String[] args)
{
int i;
for (int j = 0; j < 30; ++j)
{
i = randomNew(99,66); //效果等同于 i=randomNew(66,99);
System.out.println(i);
}
}
}
{
public static void main(String[] args)
{
int i;
int[] array = new int[32]; //33到66但不包括38和44一共有32种可能
for (int j = 0; j < 32; j++)
{
array[j] = 33 + j; //准备备选数据集
}
array[5] = 65; //原来为需要排除的38,换为缺少的65
array[11] = 66; //原来为需要排除的44,换为缺少的66 for (int j = 0; j < 30; ++j)
{
i = (int) (Math.random() * (32 - j)); //得到随机下标
System.out.println(array[i]); //得到随机数
for (int k = i; k < 32 - 1; k++)
{
array[k] = array[k + 1]; //调整备选数据(为了不取重复随机数)
}
}
}
}
int value = 0;
while (true) {
value = (int)(Math.random() * 67);
if (value>32 && value<67) {
break;
}
}
http://expert.csdn.net/Expert/topic/2953/2953628.xml?temp=.726742