#1 : 27/01-19 11:45 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 |
#2 : 27/01-19 17:45 Styb
Posts: 80
|
Reply to #1:
ADD <Inc Nr:1> on position 1. |
#3 : 27/01-19 19:16 David Lee
Posts: 1125
|
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! |
#4 : 29/01-19 14:08 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! |