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:
- Add a Code Stage to your flow.
- In the Inputs tab, create two number inputs:
MinValueandMaxValue. - In the Outputs tab, create a number output called
RandomNumber. - 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.
- In your process, drag a Action stage.
- Select the Business Object
Utility - Environment. - Choose the action
Generate Random Number. - Set the inputs:
MinimumandMaximum(both are number data items). - 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.Randominside 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 theRandomobject 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.Randomis not cryptographically secure. UseSystem.Security.Cryptography.RandomNumberGeneratorin 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:
- Create a collection with a column called
RandomValue. - Use a Loop stage to iterate over the collection.
- Inside the loop, call the
Generate Random Numberaction or a code stage. - Assign the output to the current row’s
RandomValuefield 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()orRAND()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
- Data Entry: What It Is, How It Works, and How to Get Started
- Amex Send & Split FAQ: How It Works
- Enum Tables in SQL: What They Are and How to Use Them
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Insert a Thick Horizontal Line in Microsoft WordTo add a thick horizontal line in Word, click in the paragraph where you want it, open Home…
- Excel Data Validation List: Can You Pick Multiple?Excel’s Data Validation list allows one selection at a time by default; there is no built-in “select multiple”…
- Record Inventory in Excel: A Practical Spreadsheet GuideTo record inventory in an Excel spreadsheet, give every item a unique SKU, record stock movements, and calculate…
- How to Wrap Text in Google Sheets Manually: Cell GuideTo wrap text manually in Google Sheets, select the cell or range, then choose Format > Wrapping >…
- Excel Custom Number Format: Add Leading ZerosTo show leading zeros in Excel without changing a number’s value, apply a custom number format: select the…
- Excel Learning Center Wilmington NC: Classes & TrainingExcel Learning Center in Wilmington, NC, is a local training provider offering instructor-led computer and business skills classes,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.