unique random numbers awk

Hi, I have a small piece of awk code (see below) that generates random numbers.

 
gawk -F"," 'BEGIN { srand(); for (i = 1; i <= 30; i++) printf("%s AM329_%04d\n",$0,int(36 * rand())+1) }' OFS=, AM329_hole_names.csv

The code works fine and generates alphanumeric numbers like AM329_0001, AM329_0009 etc etc. The problem is the numbers generated are not always unique. In other words I want a list of non-duplicate random numbers, no repeats. I came across some code and tried adapting it to my example. However, I can't seem to get it to work. My attempt is as follows:

 
gawk -F"," 'BEGIN { srand(); for (i = 1; i <= 30; i++) {do {select = printf("%s AM329_%04d\n",$0,int(36 * rand())+1)} while (select in pick) pick[select] = select} for (j in pick) printf("%s OR109_%04d\n", pick[j]) printf("\n")}' OFS=, AM329_hole_names.csv

Any help would be greatly appreciated.:o

Try this:

gawk -F"," 'BEGIN { srand(); for (i = 1; i <= 30; i++) { v=int(36 * rand()+1); if(v in p) i-- ; else { printf("%s AM329_%04d\n",$0,v); p[v] }}}' OFS=, AM329_hole_names.csv

Thanks Chubler_XL, that worked like a charm :wink: