---
product: Chilkat
product_version: 11.7.0
language: sqlserver
language_label: SQL Server
class: Prng
license: bundle
deprecated: false
reference_html: https://www.chilkatsoft.com/refdoc/sqlserver_PrngRef.html
---
# Chilkat Prng — SQL Server — v11.7.0

> Chilkat 11.7.0 · language=sqlserver · class=Prng
> Other versions: replace `11.7.0` in this URL. Other languages: `https://www.chilkatsoft.com/ai-docs/v/11.7.0/<lang>/classes/Prng.md`.
> Language-wide rules (install, unlock, error model, strings/bytes): `https://www.chilkatsoft.com/ai-docs/v/11.7.0/sqlserver/guide.md`.

## Purpose

**Generate random bytes, encoded values, passwords, and application identifiers.**

Use `Chilkat.Prng` when an application needs random bytes or random text values for cryptographic or application-level purposes. The class can seed itself automatically from the system entropy source, accept additional entropy from the application, and produce random data as raw bytes, encoded strings, passwords, integers, booleans, GUID-style values, or higher-level identifiers such as Firebase Push IDs.

**Random bytes**

Generate random byte data for keys, IVs, nonces, salts, tokens, test data, or protocol-specific binary values.

**Encoded random strings**

Produce random values encoded as Base64, hex, URL-safe text, or other supported encodings when binary data must be represented as text.

**Password generation**

Create random passwords using configurable character sets and length requirements.

**Entropy input**

Allow the PRNG to seed from the system entropy source and optionally add application-provided entropy.

**Random application values**

Generate random integers, booleans, GUID-style values, and other convenient random outputs for application logic.

**Firebase Push IDs**

Create Firebase-style push IDs for applications that need sortable, client-generated identifiers.

**Common pattern:** Use `Prng` to create random material in the form required by the caller: bytes for cryptographic inputs, encoded strings for tokens or API values, passwords for user-facing credentials, or Firebase Push IDs for sortable identifiers. For cryptographic workflows, generate enough random bytes for the algorithm or protocol and avoid reducing randomness by truncating or reformatting values incorrectly.

For an extended overview, see [Prng Class Overview](https://www.chilkatsoft.com/prng_class_overview.asp).

## Object creation, unlocking and error handling

**Object Creation**

```sql
DECLARE @hr int
DECLARE @prng int
EXEC @hr = sp_OACreate 'Chilkat.Prng', @prng OUT
IF @hr <> 0
BEGIN
    PRINT 'Failed to create ActiveX component'
    RETURN
END

-- ... use @prng ...

EXEC @hr = sp_OADestroy @prng
```

T-SQL uses the Chilkat ActiveX through the OLE Automation stored procedures. They must be enabled once on the server (`EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;`), and the Chilkat ActiveX registered must match the bitness of the SQL Server instance (64-bit for a 64-bit SQL Server). To bind to a specific major version of Chilkat, append the major version number to the ProgID, such as `sp_OACreate 'Chilkat.Prng.11'` for Chilkat v11.\*.\*.

`sp_OACreate` returns an object token (an `int`) that is passed as the first argument of every `sp_OAMethod`, `sp_OAGetProperty` and `sp_OASetProperty` call, and released with `sp_OADestroy`. Objects returned by methods (such as an `HttpResponse` or `JsonObject`) are also tokens received through an `int` OUT parameter; they must likewise be destroyed, and the OUT parameter is `NULL` when the method fails to return an object. Objects passed as arguments are passed by their token. When an OLE Automation procedure itself fails (non-zero `@hr`), `sp_OAGetErrorInfo` describes the error.

Data types: strings are `nvarchar(4000)`; integers, booleans (1 or 0) and object tokens are `int`; dates are `datetime`. In the signatures on this page, `@success`, `@iResult`, `@sResult` and the like are the OUT variables receiving a method's return value, `@iValue` / `@sValue` receive or supply a property value, and the remaining `@` variables are the method's arguments in order.

A string returned through an OUT parameter is limited to 4000 characters. For longer values, retrieve the result as a result set into a table variable instead of an OUT parameter, for example `DECLARE @tmp TABLE (outputLine ntext)` followed by `INSERT INTO @tmp EXEC sp_OAGetProperty @prng, 'LastErrorText'`. See [string length limitations for strings returned by sp_OAMethod calls](https://www.chilkatsoft.com/sql_server_nvarchar_max_limitation.asp).

Methods that pass or return raw byte arrays are not shown on this page, because `varbinary(max)` values cannot be exchanged through `sp_OAMethod` (see [varbinary(max) limitation](https://www.chilkatsoft.com/sql_server_varbinary_max_limitation.asp)). Use the `BinData`-based alternatives (methods ending in *Bd*) or the base64 / hex string-encoded variants instead. Binary *properties* (such as `LastBinaryResult`) can be retrieved as a result set into a table variable, as shown in their signatures. Asynchronous (*\*Async*) methods and event callbacks are not available from SQL Server.

Call `Global.UnlockBundle` once at the start of the program, before using this or any other Chilkat class. Any string unlocks a fully functional 30-day trial; a purchased unlock code is used in production. See `guide.md` for the exact code in SQL Server.

How failure is reported depends on the method's return type and is stated in the note under each method below. In every case the diagnostic text is in the `LastErrorText` property of the object whose method was called.

## Members common to every Chilkat class

Documented once per language in `common-members.md`; listed here so this page is self-contained.

| Property | Signature | Access | Meaning |
|---|---|---|---|
| `DebugLogFilePath` | `EXEC sp_OAGetProperty @prng, 'DebugLogFilePath', @sValue OUT EXEC sp_OASetProperty @prng, 'DebugLogFilePath', @sValue` | read/write | If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. |
| `LastBinaryResult` | `INSERT INTO @tmp EXEC sp_OAGetProperty @prng, 'LastBinaryResult'` | read-only | This property is mainly used in SQL Server stored procedures to retrieve binary data from the last method call that returned binary data. |
| `LastErrorHtml` | `EXEC sp_OAGetProperty @prng, 'LastErrorHtml', @sValue OUT` | read-only | Provides HTML-formatted information about the last called method or property. |
| `LastErrorText` | `EXEC sp_OAGetProperty @prng, 'LastErrorText', @sValue OUT` | read-only | Provides plain text information about the last called method or property. |
| `LastErrorXml` | `EXEC sp_OAGetProperty @prng, 'LastErrorXml', @sValue OUT` | read-only | Provides XML-formatted information about the last called method or property. |
| `LastMethodSuccess` | `EXEC sp_OAGetProperty @prng, 'LastMethodSuccess', @iValue OUT EXEC sp_OASetProperty @prng, 'LastMethodSuccess', @iValue` | read/write | Indicates the success or failure of the most recent method call: `1` means success, `0` means failure. |
| `LastStringResult` | `EXEC sp_OAGetProperty @prng, 'LastStringResult', @sValue OUT` | read-only | In SQL Server stored procedures, this property holds the string return value of the most recent method call that returns a string. |
| `LastStringResultLen` | `EXEC sp_OAGetProperty @prng, 'LastStringResultLen', @iValue OUT` | read-only | The length, in characters, of the string contained in the LastStringResult property. |
| `VerboseLogging` | `EXEC sp_OAGetProperty @prng, 'VerboseLogging', @iValue OUT EXEC sp_OASetProperty @prng, 'VerboseLogging', @iValue` | read/write | If set to `1`, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. |
| `Version` | `EXEC sp_OAGetProperty @prng, 'Version', @sValue OUT` | read-only | Version of the component/library, such as "10.1.0" |

## Member index

| Member | Kind | Signature |
|---|---|---|
| `PrngName` | property, read/write | `EXEC sp_OAGetProperty @prng, 'PrngName', @sValue OUT EXEC sp_OASetProperty @prng, 'PrngName', @sValue` |
| `AddEntropy` | method | `EXEC sp_OAMethod @prng, 'AddEntropy', @success OUT, @entropy, @encoding` |
| `ExportEntropy` | method | `EXEC sp_OAMethod @prng, 'ExportEntropy', @sResult OUT` |
| `FirebasePushId` | method | `EXEC sp_OAMethod @prng, 'FirebasePushId', @sResult OUT` |
| `GenRandom` | method | `EXEC sp_OAMethod @prng, 'GenRandom', @sResult OUT, @numBytes, @encoding` |
| `GenRandomBd` | method | `EXEC sp_OAMethod @prng, 'GenRandomBd', @success OUT, @numBytes, @bd` |
| `GetEntropy` | method | `EXEC sp_OAMethod @prng, 'GetEntropy', @sResult OUT, @numBytes, @encoding` |
| `ImportEntropy` | method | `EXEC sp_OAMethod @prng, 'ImportEntropy', @success OUT, @entropy` |
| `RandomInt` | method | `EXEC sp_OAMethod @prng, 'RandomInt', @iResult OUT, @low, @high` |
| `RandomPassword` | method | `EXEC sp_OAMethod @prng, 'RandomPassword', @sResult OUT, @length, @mustIncludeDigit, @upperAndLowercase, @mustHaveOneOf, @excludeChars` |
| `RandomString` | method | `EXEC sp_OAMethod @prng, 'RandomString', @sResult OUT, @length, @bDigits, @bLower, @bUpper` |

## Properties

### Prng.PrngName

`Chilkat 11.7.0 · sqlserver · Prng · PrngName · property · read/write · since 9.5.0.52`

```sql
EXEC sp_OAGetProperty @prng, 'PrngName', @sValue OUT
EXEC sp_OASetProperty @prng, 'PrngName', @sValue
```

Selects the pseudo-random number generator used by this object. The only supported value is `fortuna`, which is also the default. Chilkat's Fortuna implementation uses AES-256 for generation and SHA-256 in its entropy-processing and reseeding design.

**Compatibility note:** Assigning any value other than `fortuna` is ignored. Applications should normally leave this property unchanged.

See also:

- [Fortuna PRNG Synopsis](https://www.schneier.com/fortuna.html)
- [Fortuna PRNG On Wikipedia](https://en.wikipedia.org/wiki/Fortuna_%28PRNG%29)

## Methods

### Prng.AddEntropy

`Chilkat 11.7.0 · sqlserver · Prng · AddEntropy · method · since 9.5.0.52`

```sql
EXEC sp_OAMethod @prng, 'AddEntropy', @success OUT, @entropy, @encoding
```

**Full documentation for this method (same in every language):** [Supported Binary Encodings](https://www.chilkatsoft.com/ai-docs/v/11.7.0/articles/binary_encodings.md)

Adds caller-supplied seed material to the PRNG. `entropy` contains the bytes in the text encoding named by `encoding`, such as `hex`, `base64`, or `ascii`. For Fortuna, the decoded bytes are incorporated into the internal entropy pools and become available to later reseeding.

This method is optional and is not the normal way an application obtains entropy for the PRNG. It may be called more than once and at any point during the lifetime of the object. It returns `1` when the input is decoded and accepted, or `0` if the encoding is invalid or another error occurs.

**Automatic system seeding:** If no entropy has been added before the first request for random output, Chilkat automatically obtains 32 bytes from the platform's cryptographic system entropy source to seed the PRNG. This is the normal and recommended mode of operation. Call `AddEntropy` only when the application has additional trustworthy entropy to contribute or deliberately needs deterministic seeding for testing.

**Repeatable tests:** A fresh `Prng` object can be seeded with the same deterministic values to reproduce the same output sequence, provided the same entropy calls and random-generation calls occur in the same order. Predictable seed material is appropriate only for tests and debugging. Do not use it for real keys, nonces, passwords, or tokens.

Returns 1 for success, 0 for failure.

### Prng.ExportEntropy

`Chilkat 11.7.0 · sqlserver · Prng · ExportEntropy · method · since 9.5.0.52`

```sql
EXEC sp_OAMethod @prng, 'ExportEntropy', @sResult OUT
```

Exports a Base64-encoded value derived from the PRNG's accumulated entropy pools. The internal pools are rehashed for export so the returned value is not a direct disclosure of the generator state.

The returned value can be persisted and supplied to `ImportEntropy` when a later process starts. For language bindings in which this method directly returns a string, check `LastMethodSuccess` before using the result.

**Seed-file guidance:** Imported entropy can improve startup behavior, but it should not be the only seed used indefinitely. After importing, also allow Chilkat to obtain fresh operating-system entropy. Protect persisted entropy from unauthorized modification and accidental reuse across cloned systems.

Returns `NULL` on failure

### Prng.FirebasePushId

`Chilkat 11.7.0 · sqlserver · Prng · FirebasePushId · method · since 9.5.0.58`

```sql
EXEC sp_OAMethod @prng, 'FirebasePushId', @sResult OUT
```

Generates a Firebase-compatible push ID. A push ID is a 20-character identifier built from a 48-bit millisecond timestamp followed by 72 bits of randomness, encoded with Firebase's ordered 64-character alphabet.

The timestamp prefix makes push IDs sort approximately by creation time, while the random portion makes collisions highly unlikely across independent clients. For language bindings in which this method directly returns a string, check `LastMethodSuccess` before using the result.

**Not a secret:** A push ID is an identifier, not an authentication token. Its prefix reveals approximate creation time, and uniqueness is probabilistic rather than an absolute guarantee.

Returns `NULL` on failure

### Prng.GenRandom

`Chilkat 11.7.0 · sqlserver · Prng · GenRandom · method · since 9.5.0.52`

```sql
EXEC sp_OAMethod @prng, 'GenRandom', @sResult OUT, @numBytes, @encoding
```

**Full documentation for this method (same in every language):** [Supported Binary Encodings](https://www.chilkatsoft.com/ai-docs/v/11.7.0/articles/binary_encodings.md)

Generates `numBytes` random bytes and returns those bytes as text using the encoding named by `encoding`, such as `hex`, `base64`, or `base64url`. `numBytes` is the number of binary bytes before text encoding; it is not the number of output characters.

If no entropy has been added before the first request for random output, Chilkat automatically obtains 32 bytes from the platform's cryptographic system entropy source to seed the PRNG.

For language bindings in which this method directly returns a string, check `LastMethodSuccess` before using the result.

**Encoding does not add entropy:** Hex, Base64, and other encodings represent the same random bytes in different textual forms. Choose `numBytes` from the security requirement of the key, nonce, salt, or token—not from the desired text length.

Returns `NULL` on failure

### Prng.GenRandomBd

`Chilkat 11.7.0 · sqlserver · Prng · GenRandomBd · method · since 9.5.0.66`

```sql
EXEC sp_OAMethod @prng, 'GenRandomBd', @success OUT, @numBytes, @bd
```

Generates `numBytes` random bytes and appends them to the existing contents of the `BinData` object passed in `bd`. The destination is not cleared before the bytes are added.

If no entropy has been added before the first request for random output, Chilkat automatically obtains 32 bytes from the platform's cryptographic system entropy source to seed the PRNG.

Returns `1` on success or `0` on failure.

**Appending behavior:** Call `BinData.Clear` first when the destination should contain only the newly generated bytes.

Returns 1 for success, 0 for failure.

### Prng.GetEntropy

`Chilkat 11.7.0 · sqlserver · Prng · GetEntropy · method · since 9.5.0.52`

```sql
EXEC sp_OAMethod @prng, 'GetEntropy', @sResult OUT, @numBytes, @encoding
```

**Full documentation for this method (same in every language):** [Supported Binary Encodings](https://www.chilkatsoft.com/ai-docs/v/11.7.0/articles/binary_encodings.md)

Requests `numBytes` bytes from the platform's cryptographic system entropy source and returns them using the text encoding named by `encoding`. This obtains system-provided random seed material directly; it is different from asking the Fortuna generator to expand its current state with `GenRandom`.

For seeding this PRNG, 32 bytes normally provide the full 256-bit seed input expected by the implementation. Applications may request another amount when a protocol specifically requires raw bytes from the system source.

For language bindings in which this method directly returns a string, check `LastMethodSuccess` before using the result.

**Usually unnecessary:** The simplest secure use is to call a random-generation method directly. If no entropy was added explicitly, Chilkat automatically obtains 32 bytes of system entropy on the first generation request.

Returns `NULL` on failure

### Prng.ImportEntropy

`Chilkat 11.7.0 · sqlserver · Prng · ImportEntropy · method · since 9.5.0.52`

```sql
EXEC sp_OAMethod @prng, 'ImportEntropy', @success OUT, @entropy
```

Imports the Base64-encoded entropy value previously returned by `ExportEntropy`. The imported material is supplied to the PRNG for use in its entropy and reseeding process.

Returns `1` if the value is valid and imported successfully. Returns `0` if it cannot be decoded or imported; check `LastErrorText` for details.

**Recommended startup sequence:** Import persisted entropy before the first random-generation call, then mix in fresh system entropy or allow the first generation call to perform Chilkat's automatic system seeding.

Returns 1 for success, 0 for failure.

### Prng.RandomInt

`Chilkat 11.7.0 · sqlserver · Prng · RandomInt · method · since 9.5.0.52`

```sql
EXEC sp_OAMethod @prng, 'RandomInt', @iResult OUT, @low, @high
```

Returns a pseudorandom integer in the inclusive range from `low` through `high`. For example, `RandomInt(4, 8)` can return 4, 5, 6, 7, or 8.

The caller should ensure that `low` is less than or equal to `high`.

**Cryptographic material:** Use `GenRandom`, `GenRandomBytes`, or `GenRandomBd` when a protocol requires an exact number of random bytes. Do not assemble keys or nonces from decimal random integers unless the protocol explicitly defines that representation.

### Prng.RandomPassword

`Chilkat 11.7.0 · sqlserver · Prng · RandomPassword · method · since 9.5.0.52`

```sql
EXEC sp_OAMethod @prng, 'RandomPassword', @sResult OUT, @length, @mustIncludeDigit, @upperAndLowercase, @mustHaveOneOf, @excludeChars
```

Generates a password of exactly `length` characters while applying the requested composition rules:

| Argument | Effect |
| :--- | :--- |
| `mustIncludeDigit` | When `1`, require at least one digit from `0-9`. |
| `upperAndLowercase` | When `1`, require at least one lowercase and at least one uppercase US-ASCII letter. |
| `mustHaveOneOf` | When nonempty, require at least one character from this supplied set, such as `!@#$%`. |
| `excludeChars` | Exclude every listed character from the generated password. This is useful for ambiguous characters such as `iIlL1oO0`. |

The requested length must be large enough to satisfy all enabled requirements, and exclusions must not remove every usable character from a required category. For language bindings in which this method directly returns a string, check `LastMethodSuccess` before using the result.

**Password policy note:** Longer randomly generated passwords generally provide more security than short passwords with many composition rules. Also ensure the receiving system accepts every character that may be generated.

Returns `NULL` on failure

### Prng.RandomString

`Chilkat 11.7.0 · sqlserver · Prng · RandomString · method · since 9.5.0.52`

```sql
EXEC sp_OAMethod @prng, 'RandomString', @sResult OUT, @length, @bDigits, @bLower, @bUpper
```

Generates a string of exactly `length` characters. The remaining arguments select the character categories placed in the source alphabet:

| Argument | When `1` |
| :--- | :--- |
| `bDigits` | Include digits `0-9`. |
| `bLower` | Include lowercase ASCII letters `a-z`. |
| `bUpper` | Include uppercase ASCII letters `A-Z`. |

At least one character category must be enabled. The flags define the pool from which each character is chosen; they do not guarantee that every enabled category appears in a particular result.

**Requirement-aware passwords:** Use `RandomPassword` instead when the result must contain at least one digit, both uppercase and lowercase letters, or a required special character.

Returns `NULL` on failure

## Related classes

[`BinData`](https://www.chilkatsoft.com/ai-docs/v/11.7.0/sqlserver/classes/BinData.md)

