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

时间:2022-02-01 03:46:33

I have a two dimensional array 10x10 and I need to know the way to put 20 (-1)s in 20 random cells. Thats my array:

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

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

5 个解决方案

#1


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();
      }
   }
}

Output:

    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 

#2


2  

Pseudo code:

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.

#3


1  

Method randomHashSet() gives you n (20) numbers from 0 to 99. Then you can use easy math trick to map this one-dimension list to two-dimensions array.

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

Try this code:

试试这段代码:

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;
    }
}

#4


1  

  • Create one dimensional array or even better List which will represent elements from your two dimensional array.
  • 创建一维数组甚至更好的List,它将表示二维数组中的元素。

  • Set first 20 elements in list to -1.
  • 将列表中的前20个元素设置为-1。

  • Shuffle list (Collections.shuffle may be helpful) (now -1 are at random places and you know that there are 20 of them)
  • 随机播放列表(Collections.shuffle可能会有所帮助)(现在-1在随机位置,你知道其中有20个)

  • Put -1 back to two dimensional array. For instance if -1 is at position 23 place -1 in your array at position [2][3] (/10 and %10 may be helpful here).
  • 将-1放回二维数组。例如,如果-1位于位置23处,则位置[2] [3]中的数组为-1(/ 10和%10在此处可能有用)。

#5


0  

This post discusses how to get a random number between any two bounds: Java Random number between -100 and 100

这篇文章讨论了如何获得任意两个边界之间的随机数:Java随机数介于-100和100之间

The legal array indexes for your 2D-array are between 0 and 9.

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

So get a random numbers between zero and nine, twice, and use those for the array index. You could get the same set twice, so you may need to keep track of the already-chosen indicies, or if (thatelement == -1) is true, it was already set, so do it again.

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

To fill all elements with -1, you could use (pre-7) System.arraycopy(o,i,o,i,i) or (7 & 8) Arrays.copyOfRange(i[],i,i).

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

#1


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();
      }
   }
}

Output:

    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 

#2


2  

Pseudo code:

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.

#3


1  

Method randomHashSet() gives you n (20) numbers from 0 to 99. Then you can use easy math trick to map this one-dimension list to two-dimensions array.

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

Try this code:

试试这段代码:

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;
    }
}

#4


1  

  • Create one dimensional array or even better List which will represent elements from your two dimensional array.
  • 创建一维数组甚至更好的List,它将表示二维数组中的元素。

  • Set first 20 elements in list to -1.
  • 将列表中的前20个元素设置为-1。

  • Shuffle list (Collections.shuffle may be helpful) (now -1 are at random places and you know that there are 20 of them)
  • 随机播放列表(Collections.shuffle可能会有所帮助)(现在-1在随机位置,你知道其中有20个)

  • Put -1 back to two dimensional array. For instance if -1 is at position 23 place -1 in your array at position [2][3] (/10 and %10 may be helpful here).
  • 将-1放回二维数组。例如,如果-1位于位置23处,则位置[2] [3]中的数组为-1(/ 10和%10在此处可能有用)。

#5


0  

This post discusses how to get a random number between any two bounds: Java Random number between -100 and 100

这篇文章讨论了如何获得任意两个边界之间的随机数:Java随机数介于-100和100之间

The legal array indexes for your 2D-array are between 0 and 9.

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

So get a random numbers between zero and nine, twice, and use those for the array index. You could get the same set twice, so you may need to keep track of the already-chosen indicies, or if (thatelement == -1) is true, it was already set, so do it again.

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

To fill all elements with -1, you could use (pre-7) System.arraycopy(o,i,o,i,i) or (7 & 8) Arrays.copyOfRange(i[],i,i).

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