Generate Random Number in Blue Prism: Full Guide

How-to basics · Updated November 23, 2026

To generate a random number in Blue Prism, use the Math utility in the VBO (Visual Business Object) called Utility - Collection Manipulation or, more commonly, write a short C# code stage using the System.Random class. The simplest method is a Code Stage with System.Random, which gives you full control over the range and seed.

Why Blue Prism Needs Random Numbers

Blue Prism is an RPA (Robotic Process Automation) tool that automates repetitive tasks. Random numbers are not a built-in function in the core product; they are generated through code stages or VBOs. Common uses include:

  • Creating unique transaction IDs for testing or audit trails.
  • Simulating user input, such as random dates or quantities, in test environments.
  • Selecting random records from a collection to avoid processing the same row every time.
  • Adding variance to data before submitting to external systems that reject duplicate values.

Method 1: Using a Code Stage (C#)

The most direct approach is to add a Code Stage to your process or object. In the Code Stage properties, you define input and output parameters. Here is a complete example:

  1. Add a Code Stage to your flow.
  2. In the Inputs tab, create two number inputs: MinValue and MaxValue.
  3. In the Outputs tab, create a number output called RandomNumber.
  4. In the Code tab, paste this C# code:
// Create a random number between MinValue and MaxValue (inclusive)
System.Random rnd = new System.Random();
RandomNumber = rnd.Next(MinValue, MaxValue + 1); // +1 because Next upper bound is exclusive

Note that RandomNumber is a variable you declared in the Outputs tab. The +1 is essential because Next() returns a number less than the upper bound. If you want numbers from 1 to 100, set MinValue=1 and MaxValue=100, and the code uses MaxValue + 1.

Method 2: Using the Utility VBO

Blue Prism ships with a VBO called Utility - Collection Manipulation. It does not have a random number action. However, the Utility - Environment VBO has a Generate Random Number action. This is a built-in, no-code option.

  1. In your process, drag a Action stage.
  2. Select the Business Object Utility - Environment.
  3. Choose the action Generate Random Number.
  4. Set the inputs: Minimum and Maximum (both are number data items).
  5. The action outputs a single number called Result.

This action uses the .NET Random class internally. It is slightly slower than a code stage because of the VBO overhead, but it requires no coding and is easier for non-developers to read.

Choosing Between the Two Methods

Your choice depends on your environment and skill level:

  • Code Stage is faster, more flexible (you can seed the generator, generate arrays, or use cryptographic randomness), and does not depend on the VBO being present in the environment.
  • Utility – Environment is simpler to maintain for business users, and it is a standard part of Blue Prism, so it is always available.
  • If you need a random decimal (e.g., 3.14), the VBO only returns integers. You would need a code stage with rnd.NextDouble() and then scale it.

Common Problems and Pitfalls

Several issues trip up developers when generating random numbers in Blue Prism:

  • Same number every run: If you create a new System.Random inside a loop without a seed, .NET uses the system clock. In rapid loops, the clock may not change, so you get the same value. Fix: create the Random object once outside the loop, or use a static instance.
  • Off-by-one errors: Forgetting that Next(1, 10) returns 1 to 9, not 10. Always add 1 to the max if you want the max inclusive.
  • Data type mismatches: The VBO expects number data items. If you pass a text value, it throws a conversion error. Ensure your data items are type Number, not Text.
  • Randomness quality: For security-sensitive tasks (e.g., generating passwords), System.Random is not cryptographically secure. Use System.Security.Cryptography.RandomNumberGenerator in a code stage instead.

Generating a Random Number in a Loop

Often you need a random number for each row in a collection. Here is the correct pattern:

  1. Create a collection with a column called RandomValue.
  2. Use a Loop stage to iterate over the collection.
  3. Inside the loop, call the Generate Random Number action or a code stage.
  4. Assign the output to the current row’s RandomValue field using a Calculation stage.

If you use a code stage, declare a static random object at the top of the code to avoid repetition. Example:

// This code runs once per loop iteration, but uses a static seed
private static System.Random _rnd = new System.Random();
RandomNumber = _rnd.Next(1, 101);

Note: Blue Prism code stages do not support static variables across multiple invocations unless you use a global variable or a session variable. A simpler workaround is to generate all numbers at once in a single code stage that loops through the collection and assigns values, then exits.

Alternatives and Best Practices

Beyond the two main methods, consider these alternatives:

  • Use a GUID: If you need a unique identifier rather than a random number, use System.Guid.NewGuid().ToString() in a code stage. This is guaranteed unique and does not require a range.
  • Excel formula: If your process writes to Excel, you can use =RANDBETWEEN(1,100) in a cell, but this is slower and depends on Excel being open.
  • Database function: If you are using SQL, you can call NEWID() or RAND() in a query, but this couples your process to the database.

Best practices for production:

  • Always test the range boundaries. Write a small process that generates 1000 numbers and logs them to verify they stay within bounds.
  • Avoid generating random numbers in a tight loop with the VBO; batch them in a code stage for performance.
  • For audit purposes, log the seed value if you use a seeded generator, so you can reproduce the sequence if needed.

Frequently Asked Questions

Can I generate a random decimal number in Blue Prism?

Yes, but not with the standard VBO. Use a code stage with System.Random and call NextDouble(), which returns a value between 0.0 and 1.0. Multiply by your range and add the minimum. For example, to get a number between 10.0 and 20.0, use 10 + (rnd.NextDouble() * 10).

Why does my random number repeat when I run the process multiple times?

Because System.Random is seeded with the current time, and if you create a new instance each time, the seed may be identical if the time hasn’t changed (e.g., within milliseconds). The fix is to create one instance at the start of the process and reuse it, or use a different seed like Environment.TickCount.

Is the Blue Prism random number generator cryptographically secure?

No. The Utility - Environment VBO and System.Random are not secure for passwords or tokens. For security-sensitive use, write a code stage using System.Security.Cryptography.RNGCryptoServiceProvider or RandomNumberGenerator.Fill().

How do I generate a random number between 1 and 10 inclusive in Blue Prism?

Set Minimum=1 and Maximum=10 in the VBO action, or in a code stage use rnd.Next(1, 11). The upper bound is exclusive, so you must add 1 to the maximum you want.

Can I use a random number to select a random row from a collection?

Yes. Get the collection’s row count using the Utility - Collection Manipulation action Get Number of Rows, generate a random number between 1 and that count, then use the Get Row action with that index. Note that Blue Prism row indices start at 1.

Does the random number change if I run the process on different machines?

Yes, because the seed is based on the system clock, which differs. If you need consistent results across machines for testing, use a fixed seed in your code stage, like new System.Random(12345). This will produce the same sequence every time.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.