Java random()中的唯一值

Java random()中的唯一值,java,arrays,random,processing,Java,Arrays,Random,Processing,我有一个数组,其中加载了10幅图像,如下所示: int maxImages = 10; // Total # of images int imageIndex = int(random(10)); // Initial image to be displayed is the first // Declaring an array of images. PImage[] images = new PImage[maxImages]; // Loading the images into th

我有一个数组,其中加载了10幅图像,如下所示:

int maxImages = 10; // Total # of images
int imageIndex = int(random(10)); // Initial image to be displayed is the first

// Declaring an array of images.
PImage[] images = new PImage[maxImages];

// Loading the images into the array
  for (int i = 0; i < images.length; i ++ ) {
    images[i] = loadImage( "assets/images/image" + i + ".jpg" ); 
  }
imageIndex = int(random(images.length));
生成后,我用以下方式调用带有新随机数的图像:

gameimages = loadImage( "assets/images/image" + imageIndex + ".jpg" );

上面的实现工作得很好,但是随机数有时会生成多次,我希望它们从1到10唯一随机。有人能给我建议一下实现这一点的最佳方法吗?

使用集合洗牌:

ArrayList<Integer> numbers = new ArrayList<Integer>();    
for(int i = 0; i < images.length; i++) {       
    numbers.add(i+1);     
}      
Collections.shuffle(numbers);
ArrayList number=new ArrayList();
对于(inti=0;i
@fge我使用的是processing,它是一种开源编程语言和集成开发环境(IDE),专为电子艺术设计。它使用一个普通java的基础,使用一个列表而不是数组,并使用collection.shuffle一旦你的列表被填充,为什么基于java的东西会使用语法,比如
int(…)
?这是weird@njzk2你能为我的问题提供一个例子和你的解决方案吗。提前感谢
List numbers=new ArrayList(images.length)
@Toma但如何从变量
imageIndex
中的随机数生成唯一的随机数?您可以使用变量“counter”,它将遍历ArrayList编号并返回编号。get(counter)。一旦你得到一个随机数,你就会增加计数器。@Toma你能给我一个例子说明如何实现你的解决方案吗?请首先在类的顶部声明一个变量“counter”和ArrayList“numbers”。在类的构造函数中,或在其他可能的位置,shuffle ArrayList:`numbers=new ArrayList();对于(inti=0;i