生成从0到7的随机数,每个数字在java ADT中出现2次
我需要生成从0到7的随机数,每个数出现2次。最终结果应使每个数字以随机顺序出现2次。例如:生成从0到7的随机数,每个数字在java ADT中出现2次,java,random,adt,Java,Random,Adt,我需要生成从0到7的随机数,每个数出现2次。最终结果应使每个数字以随机顺序出现2次。例如: MyArray = [3, 6, 0, 5, 2, 2, 6, 7, 5, 4, 7, 1, 3, 1, 0, 4] 这就是我试图做的。代码单独工作,但不在ADT环境中(在适配器类中,访问了16次,但使用此代码时仅访问了14次) ArrayList nombres=new ArrayList(); 私有int getRandomNumber(){ Random rand=新的Random(); 内部温度
MyArray = [3, 6, 0, 5, 2, 2, 6, 7, 5, 4, 7, 1, 3, 1, 0, 4]
这就是我试图做的。代码单独工作,但不在ADT环境中(在适配器类中,访问了16次,但使用此代码时仅访问了14次)
ArrayList nombres=new ArrayList();
私有int getRandomNumber(){
Random rand=新的Random();
内部温度;
while(true){
温度=兰特耐克斯汀(8);
如果(nombres.size()==0){
标称添加(温度);
返回温度;
}
如果(标称包含(温度)){
如果(标称索引值(临时)=标称最后索引值(临时)){
标称添加(温度);
返回温度;
}
}
如果(!标称包含(温度)){
标称添加(温度);
返回温度;
}
}
}
还有其他更简单的解决方案吗?(我试着把所有的东西都放在一个if中,结果是一样的)。用这种方式使用唯一的数字通常是低效的 更好的方法是首先生成整数数组,然后使用交换操作生成随机列表:
int n = 8;//maximum bound (exclusive)
int s = 2*n;
Random rand = new Random();
int[] result = new int[s];
for(int i = 0, j = 0; i < n; i++) {
result[j++] = i;
result[j++] = i;
}
for(int i = 0; i < s; i++) {
int j = i + rand.nextInt(s-i);
int temp = result[i];
result[i] = result[j];
result[j] = temp;
}
int n=8//最大限制(独占)
int s=2*n;
Random rand=新的Random();
int[]结果=新的int[s];
对于(int i=0,j=0;i
或其对应的ArrayList
:
int n=8//最大限制(独占)
int s=2*n;
Random rand=新的Random();
ArrayList结果=新建ArrayList();
对于(int i=0,j=0;i
以这种方式使用唯一数字通常效率低下
更好的方法是首先生成整数数组,然后使用交换操作生成随机列表:
int n = 8;//maximum bound (exclusive)
int s = 2*n;
Random rand = new Random();
int[] result = new int[s];
for(int i = 0, j = 0; i < n; i++) {
result[j++] = i;
result[j++] = i;
}
for(int i = 0; i < s; i++) {
int j = i + rand.nextInt(s-i);
int temp = result[i];
result[i] = result[j];
result[j] = temp;
}
int n=8//最大限制(独占)
int s=2*n;
Random rand=新的Random();
int[]结果=新的int[s];
对于(int i=0,j=0;i
或其对应的ArrayList
:
int n=8//最大限制(独占)
int s=2*n;
Random rand=新的Random();
ArrayList结果=新建ArrayList();
对于(int i=0,j=0;i
一种简单的方法是使用Collections.shuffle()
伪代码:
for(0 to 7)
myArrayList.add(num);
myArrayList.add(num);
Collections.shuffle(myArrayList);
// Convert to array if necessary
一种简单的方法是使用
Collections.shuffle()
伪代码:
for(0 to 7)
myArrayList.add(num);
myArrayList.add(num);
Collections.shuffle(myArrayList);
// Convert to array if necessary
一种简单得多的方法是先填充结果,然后再提供结果:
List<Integer> nombres = new ArrayList<Integer>();
int index;
private int getRandomNumber() {
if (nombres.isEmpty()) {
for (int i = 0; i < 8; i++) {
nombres.add(i);
nombres.add(i);
}
Collections.shuffle(nombres);
}
if (index >= nombres.size())
throw new IllegalStateException();
return nombres.get(index++);
}
List nombres=new ArrayList();
整数指数;
私有int getRandomNumber(){
if(nombres.isEmpty()){
对于(int i=0;i<8;i++){
增加(i)项;
增加(i)项;
}
收藏。洗牌(名词);
}
如果(索引>=nombres.size())
抛出新的非法状态异常();
返回nombres.get(index++);
}
一种简单得多的方法是先填充结果,然后再提供:
List<Integer> nombres = new ArrayList<Integer>();
int index;
private int getRandomNumber() {
if (nombres.isEmpty()) {
for (int i = 0; i < 8; i++) {
nombres.add(i);
nombres.add(i);
}
Collections.shuffle(nombres);
}
if (index >= nombres.size())
throw new IllegalStateException();
return nombres.get(index++);
}
List nombres=new ArrayList();
整数指数;
私有int getRandomNumber(){
if(nombres.isEmpty()){
对于(int i=0;i<8;i++){
增加(i)项;
增加(i)项;
}
收藏。洗牌(名词);
}
如果(索引>=nombres.size())
抛出新的非法状态异常();
返回nombres.get(index++);
}
添加所有数字,然后使用可能是最简单的
List<Integer> generateRandomArray(int max) {
List<Integer> result = new ArrayList<Integer>();
for(int i = 0; i <= max; i++) {
result.add(i);
result.add(i);
}
Collections.shuffle(result);
return result;
}
List generateRandomArray(int max){
列表结果=新建ArrayList();
对于(inti=0;i,将所有数字相加,然后使用可能是最简单的
List<Integer> generateRandomArray(int max) {
List<Integer> result = new ArrayList<Integer>();
for(int i = 0; i <= max; i++) {
result.add(i);
result.add(i);
}
Collections.shuffle(result);
return result;
}
List generateRandomArray(int max){
列表结果=新建ArrayList();
对于(int i=0;i)您想问什么?ADT或随机数或两者都有问题吗?对于答案:请务必解释当前方法不起作用的原因。只需替换为(尽管更好)解决方案遗漏了一半的学习内容。你想问什么?是ADT还是随机数问题,还是两者都有问题?答案:请务必解释当前方法不起作用的原因。只需替换一个(尽管更好)解决方案遗漏了一半的学习。一个小主意:你可以检查索引
是否超过“他的”界限:)。一个小主意:你可以检查索引
是否超过“他的”界限:)。