1、使用Math类的random方法实现产生1000-9999的随机数代码如下:
int a = (int)(Math.random()*(9999-1000+1))+1000//产生1000-9999的随机数
2、使用Random类实现代码:
import java.util.Random//导入Random包
public class Ranadd {
public static void main(String[] args) {
int x//定义两变量
Random ne=new Random()//实例化一个random的对象ne
x=ne.nextInt(9999-1000+1)+1000//为变量赋随机值1000-9999
System.out.println("产生的随机数是:"+x)//输出
}
}
想法:random(0,10).
取4次,每取一次,放入list
每次放入做判定,list里是否已经有此数,如果已存在继续取。
4个string相加
然后
你可以parse成你想要的格式。
每次获取一个一位数的随机数存在一个 int[4]的整型数组,如果新获取的随机数在整型数组中已经存在了,就再获取一次(使用while循环)。然后把int[4]的数转换成四位数(这个应该简单吧,使用sum=sum*10+a[i])