import java.util.HashSet;
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)望謨免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了超過(guò)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
public class RandomTest {
public static void main(String[] args){
Random random = new Random();
Set numberSet = new HashSet();
while(numberSet.size()10){
int rand = Math.abs(random.nextInt(100));
numberSet.add(new Integer(rand)); //Set中是不能放進(jìn)重復(fù)的值的,當(dāng)它有10個(gè)時(shí),就滿足你的條件了
}
for(Iterator it=numberSet.iterator();it.hasNext();){
System.out.println(it.next());
}
}
}
完整代碼為:
public class Main {
public static void main(String[] args) {
int index = 1;
int[] redBalls = new int[6];
Random random = new Random();
boolean getMoreRed = true;
boolean getAgain;
System.out.println("開始抽取紅球!");
while (getMoreRed) {
getAgain = false;
int red = random.nextInt(36) + 1;
System.out.print("本次抽取到的紅球?yàn)椋篬" + red + "]!");
for (int i = 0; i index; i++) {
if (redBalls[i] == red) {
System.out.print("重復(fù)抽取,將重新抽取紅球");
getAgain = true;
break;
}
}
System.out.println("");
if (getAgain){
continue;
}
redBalls[index - 1] = red;
index++;
getMoreRed = index 7;
}
System.out.println("抽取到的紅球?yàn)椋?);
Arrays.sort(redBalls);
for (int redBall : redBalls) {
System.out.print(redBall + " ");
}
System.out.println("\n\n開始抽取藍(lán)球!");
System.out.println("本次抽取到的藍(lán)球?yàn)椋篬" + (random.nextInt(16) + 1) + "]!");
}
}
運(yùn)行結(jié)果:
普通抽?。?/p>
重復(fù)時(shí)抽取:
public class test {\x0d\x0a public static void main(String[] args) {\x0d\x0a //定義人名數(shù)組\x0d\x0a String [] name = {"張三","李四","王五","八神庵","不知火舞","大蛇","景天","唐雪見","李逍遙","趙靈兒"};\x0d\x0a//隨機(jī)生成數(shù)組下標(biāo)、\x0d\x0a int num = (int)(Math.random() * 1000);\x0d\x0a//對(duì)生成的隨機(jī)數(shù)進(jìn)行判斷,如果小于數(shù)組下標(biāo),就跳出循環(huán)\x0d\x0awhile (numname.length-1) {\x0d\x0a if (num
回答于?2022-11-16
public class RandomTest{
public static void main(String[] args){
int i=Math.random();//random()會(huì)自動(dòng)產(chǎn)生一個(gè)0.0-1.0的雙精度隨機(jī)數(shù)
System.out.println(i);//輸出
i=Math.random()*1000;//產(chǎn)生0-1000的雙精度隨機(jī)數(shù)
System.out.println(i);
int b=(int)(Math.random()*1000);//產(chǎn)生0-1000的整數(shù)隨機(jī)數(shù)
System.out.println(b);
}
}import java.util.random
public class RandomTest{
public static void main(String[] args){
Random random=new Random();//創(chuàng)建random對(duì)象
int intNumber=random.nextInt();//獲取一個(gè)整型數(shù)
float floatNumber=random.nextFloat();//獲取一個(gè)浮點(diǎn)數(shù)(0-1)
double doubleNumber=random.nextDouble();//獲取雙精度數(shù)(0-1)
boolean booleanNumber=random.nextBoolean();//獲取boolean數(shù)
System.out.println("intNumber:"+intNumber);
System.out.println("floatNumber:"+floatNumber);
System.out.println("doubleNumber:"+doubleNumber);
System.out.println("booleanNumber:"+booleanNumber);
}
}
Random r = new Random();
System.out.println(r.nextInt(18)+1);
自己查一下API nextInt(18) 是隨機(jī)獲取0-18之間的任一數(shù)字 ==》 nextInt(18)+1 就是 1-19之間的數(shù)字
提取random是在Math的方法里的
直接給你個(gè)1-100以內(nèi)的隨機(jī)數(shù)代碼好拉
Random r=new Random();
int i1=r.nextInt();
int i2=r.nextInt(100);//返回0到100之間的隨機(jī)數(shù)
double d=r.nextDouble();
float f=r.nextFloat();
byte[] c=new byte[10];;
r.nextBytes(c);//生成的隨機(jī)數(shù)存放在 數(shù)組c中