随机抽取java
⑴ 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,这样应该就是实现了随机抽取的效果