如何在数组中添加随机数?

时间:2014-03-29 15:29:42

标签: java arrays random

我有一个二维数组10x10,我需要知道在20个随机单元格中放置20(-1)s的方法。 那是我的阵列:

private int[][] array = new int[10][10];

5 个答案:

答案 0 :(得分:2)

伪代码:

Generate a random number for first index range [0,9]
Generate a random number for the second index range [0,9]
Check if it has already been set:
    if it has repeat until this is false
    if it hasn't, continue below
Set the location
Repeat 19 more times.

答案 1 :(得分:1)

方法randomHashSet()为您提供0到99之间的n (20)个数字。然后您可以使用简单的数学技巧将此一维列表映射到二维数组。

试试这段代码:

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class MyClass {
    static int[][] array = new int[10][10];

    public static void main(String[] args) {
        Set<Integer> numbers = MyClass.randomHashSet(20);

        for (Integer el : numbers) {
            array[el / 10][el % 10] = -1;
        }

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }

    }

    public static Set<Integer> randomHashSet(int n) {
        Set<Integer> set = new HashSet<Integer>();
        Random random = new Random();

        while (set.size() < n) {
            set.add(random.nextInt(100));
        }
        return set;
    }
}

答案 2 :(得分:1)

  • 创建一维数组甚至更好的List,它将代表二维数组中的元素。
  • 将列表中的前20个元素设置为-1
  • 随机播放列表(Collections.shuffle可能会有所帮助)(现在-1位于随机位置,您知道其中有20个)
  • -1放回二维数组。例如,如果-1位于23位置-1位于[2][3]的数组中,/10%10可能会对此有所帮助。

答案 3 :(得分:0)

本文讨论如何在任意两个边界之间获取随机数: Java Random number between -100 and 100

2D数组的合法数组索引介于0到9之间。

因此,获取0到9之间的随机数,两次,并将其用于数组索引。您可以两次获得相同的集合,因此您可能需要跟踪已经选择的指标,或者如果(thatelement == -1)为真,则它已经设置,所以再次进行。< / p>

要使用-1填充所有元素,您可以使用(pre-7)System.arraycopy(o,i,o,i,i)或(7&amp; 8)Arrays.copyOfRange(i[],i,i)

答案 4 :(得分:0)

import java.util.Random;

public class ArrayGrid
{
   public static void main(String[] args)
   {  
      int[][] ranAr = new int[10][10];

      // Complete 20 times
      for(int i = 0; i < 20; i++)
      {
         // Get random value between 0-9
         // for the array position
         Random r = new Random();
         int posOne = r.nextInt(10);
         int posTwo = r.nextInt(10);

         // Ensure the position has not already been set
         while(ranAr[posOne][posTwo] == -1)
         {
            posOne = r.nextInt(10);
            posTwo = r.nextInt(10);   
         }

         // Set value to position
         ranAr[posOne][posTwo] = -1;
      }

      // Print the grid to verify
      for(int x = 0; x < 10; x++)
      {
         for(int j = 0; j < 10; j++)
         {
            System.out.printf("%5d ", ranAr[x][j]);
         }
         System.out.println();
      }
   }
}

输出:

    0     0     0    -1     0     0     0    -1     0     0 
   -1    -1     0    -1    -1     0     0    -1     0     0 
   -1     0     0    -1     0     0     0     0     0     0 
    0     0     0     0     0     0     0     0     0     0 
   -1     0     0     0     0     0     0    -1    -1     0 
    0     0     0     0     0     0    -1     0     0     0 
   -1     0     0     0     0     0     0     0     0     0 
    0     0     0     0     0    -1    -1     0     0     0 
    0    -1    -1     0     0     0    -1    -1     0     0 
    0     0     0     0     0     0     0     0     0     0 
相关问题