title: Automatically generate random compositions by layering source images **without creating duplicates**
body: <p>This is a follow on from <a href="https://stackoverflow.com/questions/27617988/how-can-i-automatically-generate-composite-images-by-layering-source-images-rand/27621140#27621140?newreg=9c8b5b147962401a972b76169f3120e3">this question</a> asked by <a href="https://stackoverflow.com/users/4388124/ign">ign</a> and expertly answered by <a href="https://stackoverflow.com/users/2836621/mark-setchell">Marc Setchell</a></p>
  <p>It works perfectly, though I am hoping to find a way to avoid duplicates forming in the randomisation process. I am going to be doing hundreds of variations of a bunch of layers, with subtle differences, so I can't really go in and spot duplicates in a fail-safe way.</p>
  <p>Here is the code I'm using, as per above - all credit to Marc Setchell!</p>
  <pre><code>#!/bin/bash
  
  # Number of output files - edit freely :-)
  NFILES=10
  
  # Build arrays of filenames in each layer, assume directories are &quot;Layer0&quot;, &quot;Layer1&quot; etc
  IFS=$'\n' L0files=($(find &quot;Layer 0&quot; -name &quot;*.png&quot;))
  IFS=$'\n' L1files=($(find &quot;Layer 1&quot; -name &quot;*.png&quot;))
  IFS=$'\n' L2files=($(find &quot;Layer 2&quot; -name &quot;*.png&quot;))
  IFS=$'\n' L3files=($(find &quot;Layer 3&quot; -name &quot;*.png&quot;))
  
  # Produce NFILES output files
  for i in `seq 1 $NFILES`; do
  
     # Choose random index into each array of filenames
     index0=$( jot -r 1  0 $((${#L0files[@]} - 1)) )
     index1=$( jot -r 1  0 $((${#L1files[@]} - 1)) )
     index2=$( jot -r 1  0 $((${#L2files[@]} - 1)) )
     index3=$( jot -r 1  0 $((${#L3files[@]} - 1)) )
  
     # Pick up files as specified by the random index
     f0=${L0files[index0]}
     f1=${L1files[index1]}
     f2=${L2files[index2]}
     f3=${L3files[index3]}
  
     # Generate output filename, &quot;output-nnn.png&quot; 
     # ... where nnn starts at 0 and goes up till no clash
     i=0
     while :; do
        out=&quot;output-$i.png&quot;
        [ ! -f &quot;$out&quot; ] &amp;&amp; break
        ((i++))
     done
  
     echo $f0, $f1, $f2, $f3 &quot;=&gt; $out&quot;
     convert &quot;$f0&quot; &quot;$f1&quot; -composite &quot;$f2&quot; -composite &quot;$f3&quot; -composite &quot;$out&quot;
  done
  </code></pre>