Create random integers without duplicates?

edited February 2014 in How To...

I would like to display random images side by side from a list of ten images. What is the best way to avoid duplicates in selecting random images?



  • edited February 2014
    add the integers from 0 to 9 to a List
    pick one at random from the list
    delete it

    (note that the list size will decrease every time you delete something from it so write your random() call so that it uses the current list size as the maximum)

  • What have you tried? This is a pretty basic problem and it's processing, so do you need the perfect solution or could you perhaps come up with something that works? Besides, this is the kind of problem that already has a lot of answers on stackoverflow.

    Some possible solutions:

    • Put all the numbers in a list, then shuffle and move through it in sequence.
    • Put all the numbers in a list, choose one randomly, then remove it from the list.
    • Pick a random number, use it and add it to the list, pick another random number, compare it to the list, if present retry, if not present use it and add it to the list.
    • Have your objects in two lists, a toBePicked list and a alreadyPicked list. Randomly pick objects from the first list and move them to the second. In the end, the second list has all the objects in a random order.

    As usual "best way" is not that useful without some criteria to judge solutions.

  • edited February 2014 Answer ✓

    A simple example just to get you started: %%-

     * Shuffle Image Indices (v1.01)
     * by GoToLoop (2014/Feb)
     * create-random-integers-without-duplicates
    static final int NUM = 10;
    static final PImage[] imgs = new PImage[NUM];
    static final IntList indices = IntList.fromRange(NUM);
    static final String NAME = "img", EXT = ".jpg";
    void setup() {
      size(800, 600, JAVA2D);
      for ( int i = 0; i != NUM; imgs[i] = loadImage(NAME + i++ + EXT) );
    void draw() {
      final int idx = frameCount%NUM;
      if (idx == 1)  shuffleIndices();
      image(imgs[indices.get(idx)], width>>1, height>>1);
    static final void shuffleIndices() {
  • edited February 2014

    Here is my take :)

    int s = 20, t=100; // t is the range of the random numbers form 0-t
                       // s = numbers of random numbers (How many random number you want to generate)
    int[] data = new int[s]; 
    String saveMe[] = new String[s];
    ArrayList<Integer> datax = new ArrayList(); 
    void setup() {
      size(300, 100);
    void draw() {
      // generate randomo numbers
      for (int i=0;i<data.length;i++) {
        int x =(int)random(0, t+1);
        for (int j=0;j<datax.size();j++) {
          int t = (int) datax.get(j);
          if (t!=data[i]) {
            data[i] = x;
            //--just for looking coool! :) 
            text(x, i*20, height/2);
      //--Sort the data if you want or commment it
      data = sort(data); 
      //---- save the data in string array for wrting in a text file
      int l=0;
      for (int k = 0; k < data.length; k++)
        saveMe[k] = str(data[k]);
        l = l+ data[k];
      //----Take the average of all 20 values and exits from the program
      if (l/20==50) { 
        saveStrings("data.txt", saveMe);
  • A side question what is the goal of using final inside draw() loop?

  • edited February 2014

    For local variables like that idx, it's merely an annotation that it promises to keep its once-set value until the end of its scope!

  • I see, thanks.

  • Thank you so much koogs, amnon, GoToLoop, and blyk. This forum is awesome because of members like you :)>-

Sign In or Register to comment.