Share
2

Ways to generate random values in Orchestrator

by Mayank Goyal · 10 Jan 2025

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)

JavaScript
// 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)

JavaScript
// 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:

  1. In vRO, go to Library > Configuration Elements.
  2. Create a configuration element called VM_Name_Counter with an attribute called counter.

Step 2: Code to Increment the Counter:

JavaScript
// 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.

JavaScript
// 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: f47ac10b

Pros:

  • Guaranteed to be unique.
  • No need to track counters.

Cons:

  • Less readable.

📊 Summary of Options:

MethodCollision RiskLengthEase of ImplementationUse Case
Math.random() methodHighVery ShortEasiestVery short random numbers
Timestamp + Random SuffixVery LowMediumEasyGeneral use
Persistent CounterNoneShortModerateLong-running environments
UUID-Based SuffixNoneLongEasyHighly unique naming requirement


Discover more from Cloud Blogger

Subscribe to get the latest posts sent to your email.

You may also like