import java.util.Random;\x0d\x0a\x0d\x0apublic class Demo {\x0d\x0a public static void main (String args[]) {\x0d\x0a Random rnd = new Random();\x0d\x0a int[] nums = new int[25];\x0d\x0a for (int i=1; i26; i++) {\x0d\x0a int p = rnd.nextInt(25);\x0d\x0a if (nums[p] != 0)\x0d\x0a i--;\x0d\x0a else\x0d\x0a nums[p] = i;\x0d\x0a }\x0d\x0a for (int i=0; i25; i++) {\x0d\x0a System.out.print(nums[i] + " ");\x0d\x0a if (i % 5 == 4)\x0d\x0a System.out.println();\x0d\x0a }\x0d\x0a }\x0d\x0a}
創(chuàng)新互聯(lián)長期為近1000家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為分宜企業(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站設計、成都外貿(mào)網(wǎng)站建設,分宜網(wǎng)站改版等技術(shù)服務。擁有十年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
private????static?void?RandomTest()?{
Random?rand=new?Random();
int?num=0;
int?save[]=new?int[6];
int?i=1;
while(i6)
{
num=(int)(rand.nextDouble()*(100000-10000)+10000);
save[i]=num;//放入數(shù)組
System.out.println("第"+i+"個數(shù):"+save[i]);//從數(shù)組拿出
i++;
}
}
import java.util.List;
import java.util.Random;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author david
*/
public class TestRandom {
public static void main(String[] args){
List list=new ArrayList();
Random r=new Random();
int ikey=0;
while(true){
ikey=r.nextInt(60);//隨機生成小于60的整數(shù)
if(ikey20) //如果生成的整數(shù)小于20,則重新生成
continue;
if(!list.contains(ikey)){ //判斷這個隨機數(shù)是否已經(jīng)生成過,避免重復
list.add(ikey);
if(list.size()==5)
break;
}
}
//打印
for(int i=0;ilist.size();i++)
System.out.println(list.get(i));
}
}
測試過了,5位隨機數(shù),數(shù)字中不包含4
import java.util.Random;
public class Test3 {
public String test(int num) {
String str = num + "";// 轉(zhuǎn)化為字符串
for (int i = 0; i str.length(); i++) { // 遍歷str將每一位數(shù)字添加如intArray
char ch = str.charAt(i);
String strCh = ch + "";
if (strCh.equals("4")) { // 如果包含4則再次隨機
Random rnd = new Random();
int num1 = rnd.nextInt(89999) + 10000;
this.test(num1);
}
}
return str;
}
public static void main(String[] args) {
Test3 test3 = new Test3();
Random rnd = new Random();
int num = rnd.nextInt(89999) + 10000;
System.out.println("num=" + test3.test(num));
}
}