當前位置:首頁 » 編程語言 » 隨機抽取java

隨機抽取java

發布時間: 2022-06-22 05:12:18

java怎麼隨機抽取多個不重復的字元串數據

publicclassTest1{
publicstaticvoidmain(String[]args){
String[]scc={"a","b","c","d","e","f","g","h","ddd"};
Randomran=newRandom();
inti=scc.length;
intcount=0;
StringBuildersb=newStringBuilder();
while(true){
Strings=scc[ran.nextInt(i)];
if(sb.indexOf(s)==-1){
sb.append(s);
count++;
System.out.println("第"+count+"次抽到"+s);
}
if(count==5){
break;
}
}
}
}

⑵ Java怎麼用隨機抽取資料庫的數據

取ID作為查詢結果,成為ID列表,然後通過程序語言的隨機數生成兩個可用的隨機數然後用隨機數從列表中取ID,然後再查資料庫

⑶ java怎樣隨機選取一個集合裡面的數

java中隨機選取一個集合裡面的數方法:
很簡單,list.get((int)(Math.Random()*list.size()));

2、集合是Set的話:
Object[] obj =set.toArray();
obj[(int)(Math.Random()*obj.length)]

⑷ java中集合元素隨機抽取有沒有什麼好方法

importjava.util.ArrayList;
importjava.util.List;
importjava.util.Random;

publicclassTest{
publicstaticvoidmain(String[]args){
List<Object>list=newArrayList<>();//任意的集合,這里以list為例

Randomrandom=newRandom();
intrandomIndex=random.nextInt(list.size());//生成一個隨機下標

Objectobject=list.get(randomIndex);//隨機抽取的元素
}
}

⑸ java怎麼使用隨機抽取數組里的數據

package reptile;

import java.util.Random;

public class Test
{

public static void main(String[] args){
String[] b = {"公共的", "私有的", "受保護的"};
Random rand = new Random();
int num = rand.nextInt(3);
System.out.println(b[num]);
}

⑹ Java隨機抽取人名完整代碼是什麼

public class test {
public static void main(String[] args) {
//定義人名數組
String [] name = {"張三","李四","王五","八神庵","不知火舞","大蛇","景天","唐雪見","李逍遙","趙靈兒"};
//隨機生成數組下標、
int num = (int)(Math.random() * 1000);
//對生成的隨機數進行判斷,如果小於數組下標,就跳出循環
while (num>name.length-1) {
if (num<=name.length-1) {
break;
}
num = (int)(Math.random() * 1000);
}
//將數組下標設置成隨機數,就可以實現人名的隨機抽取
System.out.println(「被抽到的同學是:」+name[num]);
}
}

⑺ java:隨機抽取100內五個隨機數

你要努力啊!這種程序還要來問.這個代碼你參考一下吧!多看jdk文檔,裡面的很多東西包裝的很好的.
import java.util.HashSet;

public class Test {
public static void main(String args[]) {
HashSet<Integer> hs = new HashSet<Integer>();
while (true) {
int a = (int)(Math.random() * 100);
if(a >= 10 && a <= 100) {
hs.add(a);
}
if (hs.size() == 5) {
break;
}
}
System.out.println(hs);
}

溫馨提示:親 答題不易解題更難 您的支持是我繼續答題的動力 麻煩採納 謝謝

⑻ 請問用java從1-33個整數中隨機抽取6個數字 且不重復 1-16隨機抽取一個數,給小球

完整代碼為:

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("本次抽取到的紅球為:[" + red + "]!");
for (int i = 0; i < index; i++) {
if (redBalls[i] == red) {
System.out.print("重復抽取,將重新抽取紅球");
getAgain = true;
break;
}
}
System.out.println("");
if (getAgain){
continue;
}
redBalls[index - 1] = red;
index++;
getMoreRed = index < 7;
}
System.out.println("抽取到的紅球為:");
Arrays.sort(redBalls);
for (int redBall : redBalls) {
System.out.print(redBall + " ");
}

System.out.println(" 開始抽取藍球!");
System.out.println("本次抽取到的藍球為:[" + (random.nextInt(16) + 1) + "]!");
}
}

⑼ 關於Java的隨機抽取

Math.random()方法能返回一個介乎0到1的隨機小數,利用他就可以實現隨機取值
一般情況下,隨機演算法的格式如下:
Math.random() * (末項 - 首項) + 首項
即產生一個從0到他們的差別值的隨機數,再加上首項,就可以得出在一定范圍內的隨機數~
(int)Math.random() * (127 - 101) + 101;

int r1 = (int)Math.random() * (127 - 101) + 101;
int r2 = (int)Math.random() * (228 - 201) + 201;
int r3 = (int)Math.random() * (329 - 301) + 301;
int[] rr = {r1,r2,r3};
int result = rr[(int)Math.random() * (2 - 0) +0];
System.out.println("隨機結果是:"+result);

⑽ Java利用數組隨機抽取幸運觀眾如何實現

我的思路是這樣:
先將數組中的數據填好,然後每個索引會對應一個數據.生成一個數組長度范圍內的隨機數,用生成的隨機數作為索引來獲取索引對應的value,這樣應該就是實現了隨機抽取的效果

熱點內容
python時間毫秒數 發布:2025-02-05 20:51:32 瀏覽:329
clash安卓如何切換節點 發布:2025-02-05 20:48:20 瀏覽:889
怎樣能用到方舟編譯器 發布:2025-02-05 20:47:04 瀏覽:366
資料庫的演算法 發布:2025-02-05 20:25:32 瀏覽:859
微信解壓異常 發布:2025-02-05 20:24:39 瀏覽:493
linux0位元組文件夾 發布:2025-02-05 20:23:07 瀏覽:652
專題的腳本怎麼寫 發布:2025-02-05 20:19:18 瀏覽:923
獨立站買什麼伺服器 發布:2025-02-05 20:13:24 瀏覽:296
android鬧鍾設置 發布:2025-02-05 20:12:29 瀏覽:955
計算機代碼經典編程 發布:2025-02-05 19:25:09 瀏覽:757