Here are some robust approaches to generating a unique random value for whatever your use cases can be either generating VM names or unique IDs of some sort in Orchestrator. I’ll provide four methods with pros and cons for each method.
Method 1: Math.random() method (Short and Simple)
// Generate a short, random 3-digit number as a suffix
var randomSuffix = ("00" + Math.floor(Math.random() * 999)).slice(-3);
// Example Output: 007, 123, 987
</code>Pros:
- Very short, compact, and simple to implement.
- Suitable if naming length is restricted.
Cons:
- High chance of collision in environments with frequent deployments.
- No persistence or memory of previous numbers.
- May require additional checks to avoid duplicity.
Method 2: Timestamp + Random Suffix (Robust and Simple)
// Generate a unique incremental value using a timestamp + random 3-digit suffix
var timestamp = new Date().getTime(); // Milliseconds since 1970
var randomSuffix = ("00" + Math.floor(Math.random() * 999)).slice(-3); // Random 3-digit number
var incrementalValue = timestamp + randomSuffix;
// Example Output: 1704789992651789 (Timestamp + Random Suffix)Pros:
- Extremely low chance of collision.
- Doesn’t require external dependencies (like a database or file).
Cons:
- The name can get longer if you need compact names.
Method 3: Counter Stored in a Persistent vRO Configuration (Most Reliable)
Use a vRO configuration element to store and increment a counter for unique VM names.
Step 1: Create a Configuration Element:
- In vRO, go to
Library > Configuration Elements. - Create a configuration element called
VM_Name_Counterwith an attribute calledcounter.
Step 2: Code to Increment the Counter:
// Retrieve and increment the counter from a configuration element
var configElement = Server.getConfigurationElementCategoryWithPath("Your/Config/Path").getConfigurationElement("VM_Name_Counter");
var counter = configElement.getAttributeWithKey("counter").value;
// Increment the counter
counter = counter + 1;
// Update the configuration element with the new counter value
configElement.setAttributeWithKey("counter", counter);
// Convert the counter to a 4-digit string
var incrementalValue = ("000" + counter).slice(-4);
// Example Output: 0001, 0002, 0003...Pros:
- Fully unique and persistent across workflow runs.
- Easy to manage and track the counter value.
Cons:
- Requires managing the configuration element in Orchestrator.
Method 4: UUID-Based Suffix (Globally Unique)
If you need a completely unique identifier, use a UUID or a shortened version of it.
// Generate a UUID-based suffix
var uuid = System.getModule("com.vmware.library.vc.uuid").generateUuid();
var incrementalValue = uuid.substring(0, 8); // Use the first 8 characters
// Example Output: f47ac10bPros:
- Guaranteed to be unique.
- No need to track counters.
Cons:
- Less readable.
📊 Summary of Options:
| Method | Collision Risk | Length | Ease of Implementation | Use Case |
|---|---|---|---|---|
| Math.random() method | High | Very Short | Easiest | Very short random numbers |
| Timestamp + Random Suffix | Very Low | Medium | Easy | General use |
| Persistent Counter | None | Short | Moderate | Long-running environments |
| UUID-Based Suffix | None | Long | Easy | Highly unique naming requirement |
Discover more from Cloud Blogger
Subscribe to get the latest posts sent to your email.











function randomNumber(min,max){
var maxNum = max + 1;
var num = Math.floor(Math.random()*(maxNum – min)+min);
//System.log(“Random Number between “+min+” – “+max+”: ” + num);
return num;
}
/* Sample script runs function many times
var x = 0;
while (x!=10000){
var n = randomNumber(min,max)
System.debug(x +”: Generated Random Number: “+n);
if(n max){
throw “Invalid value: “+n;
}
x++;
}
*/
Generating random password-> function getNum() {
// Generate a random number between 33 and 126
return ((parseInt((Math.random()) * 1000) % 94) + 33);
}
function isPunctuation(num) {
// ASCII characters 33-47, 58-64, 91-96 and 123-126 are punctuation
if (((num >=33) && (num =58) && (num =91) && (num =123) && (num <=126))) { return true; }
return false;
}
// Initialise a string
var strPassword = "";
// Count over the password length
for (i=0; i < passwordLength; i++) {
// Get a random number
nextCharacter = getNum();
// If we are excluding punctuation
if (excludePunctuation) {
// Check if the random number is punctuation and get a new one until it's not
while (isPunctuation(nextCharacter)) {
nextCharacter = getNum();
}
}
// Add the character to the string password
strPassword = strPassword + String.fromCharCode(nextCharacter);
}
// Write the password to the log – for testing only!
System.log("Secure password generated: " + strPassword);
// Assign the string to the secureString Output parameter.
generatedPassword = strPassword;