Add unique random number

Advanced Renamer forum
#1 : 27/01-19 11:45
Rob
Rob
Posts: 2
Hello.
I want to add a unique number at the beginning of 47 filenames.
I use ADD with <Rand:1:47> on position 1

It works, but not all the numbers are unique. Some numbers are used several times, some numbers are not used at all. How can I get 47 different random numbers?

Regards, Rob


27/01-19 11:45
#2 : 27/01-19 17:45
Styb
Styb
Posts: 4
Reply to #1:
ADD
<Inc Nr:1> on position 1.


27/01-19 17:45
#3 : 27/01-19 19:16
David Lee
David Lee
Posts: 106
Reply to #1:
The problem with <Rand:1:47> is that EACH number is randomly selected independently so that the probability of returning 47 different numbers is approx 6.7×10^-20 ie virtually zero!

<Inc Nr:1> won't help either as this will merely return incrementing numbers from 1 to 47.

You will have to use a script. The easiest way is to generate random numbers between 1 and 47 but reject any that have already been assigned.

First create an array and initialize to zero & size 47 in the Pre batch script:

var test = [0];
for (n = 0; n < 47; n++) {
test[n] = 0;
}

then - in the main script window:

do {
x = Math.floor(Math.random() *47);
}
while (test[x] == 1);
test[x] = 1;
return item.name + "_" + ("0" + (x + 1)).slice(-2);

I've added a leading zero to make all numbers 2 digits long and "_" as a separator.

That was rather more "interesting" to sort out than I expected!


27/01-19 19:16 - edited 27/01-19 19:18
#4 : 29/01-19 14:08
Rob
Rob
Posts: 2
Reply to #3:
Wow, wouldn't have figured that out myself.... I will try this a.s.a.p. Thanks!

Update: did it and works great. A script to remember. Thanks very much!


29/01-19 14:08 - edited 29/01-19 14:21