UTIL_Random
Class · Group: Utilities
global inherited sharing class UTIL_RandomGenerates random values across multiple data types for testing and development, including numbers, strings, UUIDs, and mock Salesforce IDs.
Example
String uuid = UTIL_Random.randomUUID();
String email = UTIL_Random.randomEmail();
Integer number = UTIL_Random.nextInteger(1, 100);
Id mockAccountId = UTIL_Random.randomId(Account.SObjectType);Methods
| Method | Description |
|---|---|
| global static Boolean nextBoolean() | Produces a random boolean by mapping a binary integer result to true or false. |
| global static Integer nextInteger(Integer upperBound) | Produces a random integer from zero up to the given ceiling. |
| global static Integer nextInteger(Integer lowerBound, Integer upperBound) | Produces a random integer between the specified floor and ceiling, both inclusive. |
| global static String random(Integer length) | Builds a random string of the requested length from the printable ASCII character set (codes 32-126). |
| global static String random(Integer length, String characterSet) | Builds a random string of the requested length by sampling from the supplied character set. |
| global static String randomAlphabetic(Integer length) | Builds a random string of the requested length using only Latin letters (a-z, A-Z). |
| global static String randomAlphanumeric(Integer length) | Builds a random string of the requested length using Latin letters and decimal digits (a-z, A-Z, 0-9). |
| global static Date randomDate() | Produces a random date falling between the Unix epoch (1970-01-01) and the current day. |
| global static Datetime randomDateTime() | Produces a random datetime falling between the Unix epoch (1970-01-01) and the current moment. |
| global static Decimal randomDecimal() | Produces a random decimal value with two digits after the decimal point. |
| global static Decimal randomDecimal(Integer scale) | Produces a random decimal with a standard digit count and the caller-specified fractional precision. |
| global static Decimal randomDecimal(Integer length, Integer scale) | Produces a random decimal with a caller-specified whole-number digit count and fractional precision. |
| global static String randomDescription() | Produces a random alphabetic string exactly 40 characters long, suitable for description fields. |
| global static String randomEmail() | Produces a random, realistic-looking email address by combining name components with a randomly selected provider domain. |
| global static Id randomId(SObjectType sObjectType) | Constructs a mock 18-character Salesforce ID based on the key prefix of the supplied SObjectType, useful for unit tests that need fake record IDs. |
| global static Integer randomInteger() | Produces a random integer with a standard length of 6 digits. |
| global static Integer randomInteger(Integer upperBound) | Produces a random non-negative integer up to the specified ceiling. |
| global static Integer randomIntegerOfLength(Integer requiredLength) | Produces a random integer with exactly the specified number of digits. |
| global static String randomName() | Produces a random alphabetic string with a standard length of 10 characters. |
| global static String randomNumber() | Produces a random numeric string with a standard length of 6 digits. |
| global static String randomNumeric(Integer length) | Builds a random string of the requested length using only decimal digit characters (0-9). |
| global static String randomPhoneNumber() | Produces a random international phone number in the format '+[countryCode] ([areaCode]) [part1]-[part2]'. |
| global static String randomUUID() | Produces a universally unique identifier in the standard 8-4-4-4-12 hexadecimal format. |
nextBoolean
global static Boolean nextBoolean()Produces a random boolean by mapping a binary integer result to true or false.
Returns Boolean — A randomly selected boolean value.
Example
Boolean randomBool = UTIL_Random.nextBoolean();
System.debug(randomBool); // Outputs: true or falsenextInteger
global static Integer nextInteger(Integer upperBound)Produces a random integer from zero up to the given ceiling.
Parameters
| Parameter | Type | Description |
|---|---|---|
upperBound | Integer | The maximum value (inclusive). |
Returns Integer — An integer between 0 and the absolute value of upperBound.
Example
Integer randomInt = UTIL_Random.nextInteger(100);
System.debug(randomInt); // Outputs: e.g., 42global static Integer nextInteger(Integer lowerBound, Integer upperBound)Produces a random integer between the specified floor and ceiling, both inclusive.
Parameters
| Parameter | Type | Description |
|---|---|---|
lowerBound | Integer | The minimum value (inclusive). |
upperBound | Integer | The maximum value (inclusive). |
Returns Integer — An integer within the specified boundaries.
Example
Integer randomInt = UTIL_Random.nextInteger(10, 20);
System.debug(randomInt); // Outputs: e.g., 15random
global static String random(Integer length)Builds a random string of the requested length from the printable ASCII character set (codes 32-126).
Parameters
| Parameter | Type | Description |
|---|---|---|
length | Integer | How many characters the result should contain. |
Returns String — A random printable-ASCII string, or an empty string when length is non-positive.
Example
String randomString = UTIL_Random.random(10);
System.debug(randomString); // Outputs: e.g., "Kj#9mP$2vQ"global static String random(Integer length, String characterSet)Builds a random string of the requested length by sampling from the supplied character set.
Parameters
| Parameter | Type | Description |
|---|---|---|
length | Integer | How many characters the result should contain. |
characterSet | String | The pool of characters to draw from. |
Returns String — A random string composed of characters from the set, or an empty string when length is non-positive.
Example
String randomCustom = UTIL_Random.random(5, 'ABC');
System.debug(randomCustom); // Outputs: e.g., "ABCCB"randomAlphabetic
global static String randomAlphabetic(Integer length)Builds a random string of the requested length using only Latin letters (a-z, A-Z).
Parameters
| Parameter | Type | Description |
|---|---|---|
length | Integer | How many characters the result should contain. |
Returns String — A random alphabetic string, or an empty string when length is non-positive.
Example
String randomAlpha = UTIL_Random.randomAlphabetic(6);
System.debug(randomAlpha); // Outputs: e.g., "KjmPqR"randomAlphanumeric
global static String randomAlphanumeric(Integer length)Builds a random string of the requested length using Latin letters and decimal digits (a-z, A-Z, 0-9).
Parameters
| Parameter | Type | Description |
|---|---|---|
length | Integer | How many characters the result should contain. |
Returns String — A random alphanumeric string, or an empty string when length is non-positive.
Example
String randomAlphanumeric = UTIL_Random.randomAlphanumeric(8);
System.debug(randomAlphanumeric); // Outputs: e.g., "Kj9mP2vQ"randomDate
global static Date randomDate()Produces a random date falling between the Unix epoch (1970-01-01) and the current day.
Returns Date — A Date that is never in the future.
Example
Date randomDate = UTIL_Random.randomDate();randomDateTime
global static Datetime randomDateTime()Produces a random datetime falling between the Unix epoch (1970-01-01) and the current moment.
Returns Datetime — A Datetime that is never in the future.
Example
Datetime randomDt = UTIL_Random.randomDateTime();randomDecimal
global static Decimal randomDecimal()Produces a random decimal value with two digits after the decimal point.
Returns Decimal — A Decimal such as 45.67.
Example
Decimal randomDec = UTIL_Random.randomDecimal();global static Decimal randomDecimal(Integer scale)Produces a random decimal with a standard digit count and the caller-specified fractional precision.
Parameters
| Parameter | Type | Description |
|---|---|---|
scale | Integer | The number of digits after the decimal point. |
Returns Decimal — A Decimal with the requested precision.
Example
Decimal randomDec = UTIL_Random.randomDecimal(2);global static Decimal randomDecimal(Integer length, Integer scale)Produces a random decimal with a caller-specified whole-number digit count and fractional precision.
Parameters
| Parameter | Type | Description |
|---|---|---|
length | Integer | The magnitude of digits before the decimal point. |
scale | Integer | The number of digits after the decimal point. |
Returns Decimal — A Decimal with the requested scale.
Example
Decimal randomDec = UTIL_Random.randomDecimal(4, 2);randomDescription
global static String randomDescription()Produces a random alphabetic string exactly 40 characters long, suitable for description fields.
Returns String — A 40-character alphabetic string.
Example
String randomDesc = UTIL_Random.randomDescription();randomEmail
global static String randomEmail()Produces a random, realistic-looking email address by combining name components with a randomly selected provider domain.
Returns String — A randomly composed email address such as 'john.doe@gmail.com'.
Example
String randomEmail = UTIL_Random.randomEmail();randomId
global static Id randomId(SObjectType sObjectType)Constructs a mock 18-character Salesforce ID based on the key prefix of the supplied SObjectType, useful for unit tests that need fake record IDs.
Parameters
| Parameter | Type | Description |
|---|---|---|
sObjectType | SObjectType | The SObject type whose key prefix seeds the mock ID. |
Returns Id — A synthetic 18-character Salesforce ID.
Throws
| Exception | Description |
|---|---|
| IllegalArgumentException | When the SObjectType does not yield a valid 3-character key prefix. |
Example
Id mockId = UTIL_Random.randomId(Account.SObjectType);
System.debug(mockId); // Outputs: e.g., "00100000000lACzAAM"randomInteger
global static Integer randomInteger()Produces a random integer with a standard length of 6 digits.
Returns Integer — A 6-digit random integer such as 789234.
Example
Integer randomInt = UTIL_Random.randomInteger();global static Integer randomInteger(Integer upperBound)Produces a random non-negative integer up to the specified ceiling.
Parameters
| Parameter | Type | Description |
|---|---|---|
upperBound | Integer | The inclusive maximum for the generated value. |
Returns Integer — A random integer between 0 and upperBound, or null when upperBound is null.
Example
Integer randomInt = UTIL_Random.randomInteger(100);
System.debug(randomInt); // Outputs: e.g., 42randomIntegerOfLength
global static Integer randomIntegerOfLength(Integer requiredLength)Produces a random integer with exactly the specified number of digits.
Parameters
| Parameter | Type | Description |
|---|---|---|
requiredLength | Integer | The exact digit count for the generated integer. |
Returns Integer — An integer guaranteed to have the requested number of digits.
Example
Integer randomInt = UTIL_Random.randomIntegerOfLength(5);randomName
global static String randomName()Produces a random alphabetic string with a standard length of 10 characters.
Returns String — A 10-character alphabetic string.
Example
String randomName = UTIL_Random.randomName();randomNumber
global static String randomNumber()Produces a random numeric string with a standard length of 6 digits.
Returns String — A 6-character numeric string.
Example
String randomNum = UTIL_Random.randomNumber();randomNumeric
global static String randomNumeric(Integer length)Builds a random string of the requested length using only decimal digit characters (0-9).
Parameters
| Parameter | Type | Description |
|---|---|---|
length | Integer | How many characters the result should contain. |
Returns String — A random numeric string, or an empty string when length is non-positive.
Example
String randomNum = UTIL_Random.randomNumeric(5);
System.debug(randomNum); // Outputs: e.g., "39281"randomPhoneNumber
global static String randomPhoneNumber()Produces a random international phone number in the format '+[countryCode] ([areaCode]) [part1]-[part2]'.
Returns String — A formatted phone number such as '+123 (456) 789-0123'.
Example
String randomPhone = UTIL_Random.randomPhoneNumber();randomUUID
global static String randomUUID()Produces a universally unique identifier in the standard 8-4-4-4-12 hexadecimal format.
Returns String — A freshly generated UUID string.
Example
String uuid = UTIL_Random.randomUUID();
System.debug(uuid); // Outputs: e.g., "550e8400-e29b-41d4-a716-446655440000"