IRONSOFTWAREHOME
.NET HELP

RandomNumberGenerator C#

Jacob Mellor, Chief Technology Officer @ Team Iron
Jacob Mellor
Updated: August 1, 2026

Sometimes, when working with PDF documents, you might find yourself needing to generate random numbers or random strings. Whether you're generating random numbers and passwords for PDF encryption, creating unique file names, avoiding predictable sequences, or need to generate numbers for any other reason, using the RandomNumberGenerator C# class can help take your PDF generation and editing projects to the next level.

Unlike a basic random object created from the Random class, this cryptographic random number generator produces cryptographically strong random values suitable for cryptographic operations in secure applications.

In this article, we'll explore:

  • What RandomNumberGenerator is and why it matters.
  • How to use a cryptographic random number generator in C# to generate random numbers, random strings, and other random data for cryptographic purposes.
  • Practical examples of integrating RandomNumberGenerator with IronPDF to create secure, unique PDFs using the generated numbers and strings.
  • Tips and best practices to boost security and professionalism in your PDF applications.

What is the RandomNumberGenerator Class

Before diving into IronPDF integration, let's briefly revisit what makes RandomNumberGenerator special.

  • It is part of the System.Security.Cryptography namespace.
  • It generates cryptographically strong random values as random bytes, much more secure than the typical Random class.
  • It's ideal for scenarios requiring unpredictability, such as generating secure tokens, keys, salts, and unique identifiers.
  • Uses Cryptographic algorithms like AES-CTR DRBG, Fortuna, or OS-provided CSPRNGs. Making it resistant to prediction.
  • Best used for tasks such as creating cryptographic keys, password generation, secure tokens, unique document IDs.

This strength comes from relying on the operating system's underlying secure random number generators, making it resistant to prediction or reverse-engineering attacks. Unlike pseudo-random number generators that may produce the same sequence with the same seed value, this class is designed for true randomness and cryptographic purposes.

Why Use RandomNumberGenerator Instead of C#'s Random Class?

Many developers start with C#'s Random class for generating random integers, but it's not designed for high-security scenarios. Patterns in its output can be predicted, especially if the same seed value or system time is used, meaning the numbers generated could be guessed. This happens because the method generates values using simple modular arithmetic operations that can repeat with the same input.

In contrast, RandomNumberGenerator produces truly random numbers, derived from cryptographic random number generators in the .NET Framework or underlying OS. This ensures no low value bias and often uses a discard and retry strategy to maintain uniform distribution between a lower bound (e.g., int minValue) and an exclusive upper bound (e.g., int maxValue). The illustration below highlights the difference between a weak RNG and a secure one.

"Weak" vs. Secure RNG

Why Use RandomNumberGenerator with IronPDF

IronPDF is a robust .NET PDF library, enabling developers to create, edit, and secure PDF documents. Common use cases where randomness matters include:

  • Unique Document Identifiers: Attach cryptographically secure IDs to PDFs for tracking or validation.
  • Secure Watermarks: Embed random watermarks or codes that deter forgery.
  • Encryption Keys or Passwords: Generate secure keys or passwords for PDF encryption.
  • Random Content: Add random unique tokens or salts to PDF documents for integrity verification.

How to Generate Secure Random Data in C#

Here's a simple example of generating a 128-bit (16-byte) secure random token.

using System;
using System.Security.Cryptography;

public static string GenerateSecureToken(int size = 16)
{
    byte[] randomBytes = new byte[size];
    RandomNumberGenerator.Fill(randomBytes);
    return Convert.ToBase64String(randomBytes);
}

This method creates a secure byte array and returns it as a Base64 string - perfect for embedding or tracking in unit tests, filenames, or secure IDs.

Practical Example: Adding Unique Document IDs to PDF Documents

Let's combine the power of RandomNumberGenerator and IronPDF to generate a PDF with a unique, secure document ID stamped on each page.

Step 1: Generate a Secure Document ID

string GenerateDocumentId()
{
    byte[] idBytes = new byte[12]; // 96-bit ID
    RandomNumberGenerator.Fill(idBytes);
    return BitConverter.ToString(idBytes).Replace("-", "");
}

This produces a 24-character hex random string (e.g., "4F3A2C9B7D1E8F0A5B6C7D8E").

Step 2: Create the PDF and Stamp the ID

using IronPdf;

void CreatePdfWithSecureId()
{
    var documentId = GenerateDocumentId();

    var renderer = new ChromePdfRenderer();

    // Add a custom footer with the document ID
    renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
    {
        HtmlFragment = $"<div style='text-align: center; font-size: 10px;'>Document ID: {documentId} - Generated on {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</div>",
        DrawDividerLine = true,
    };
    var pdf = renderer.RenderHtmlAsPdf($"<h1>Secure Document</h1><p>Document ID: {documentId}</p>");

    string outputPath = $"SecurePdf_{documentId}.pdf";
    pdf.SaveAs(outputPath);

    Console.WriteLine($"PDF saved as {outputPath}");
}What this Code Does:
  • Generates a random number for a cryptographically secure, unique document ID.
  • Renders an HTML to PDF embedding that ID.
  • Adds a footer on every page with the document ID and timestamp.
  • Saves the PDF using the ID in the filename for easy tracking.

Full Working Code Example

using IronPdf;
using IronPdf.Editing;
using System;
using System.Security.Cryptography;

class Program
{
    public static void Main(string[] args)
    {
        // Create an instance of Program to run non-static methods
        var program = new Program();
        program.CreatePdfWithSecureId()
    }

    string GenerateDocumentId()
    {
        byte[] idBytes = new byte[12]; // 96-bit ID
        RandomNumberGenerator.Fill(idBytes);
        return BitConverter.ToString(idBytes).Replace("-", "");
    }

    void CreatePdfWithSecureId()
    {
        var documentId = GenerateDocumentId();

        var renderer = new ChromePdfRenderer();
        // Add a custom footer with the document ID
        renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
        {
            HtmlFragment = $"<div style='text-align: center; font-size: 10px;'>Document ID: {documentId} - Generated on {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</div>",
            DrawDividerLine = true,
        };
        var pdf = renderer.RenderHtmlAsPdf($"<h1>Secure Document</h1><p>Document ID: {documentId}</p>");

        string outputPath = $"SecurePdf_{documentId}.pdf";
        pdf.SaveAs(outputPath);

        Console.WriteLine($"PDF saved as {outputPath}");
    }
}

Output

PDF with Document ID

Advanced Use Case: Secure PDF Encryption with Random Keys

IronPDF supports PDF encryption, providing strong support for secure PDF creation. You can use RandomNumberGenerator to create strong passwords or keys for encryption:

using IronPdf;
using IronPdf.Editing;
using System;
using System.Security.Cryptography;

class Program
{
    public static void Main(string[] args)
    {
        // Create an instance of Program to run non-static methods
        var program = new Program();
        program.CreateEncryptedPdf();
    }

    string GenerateSecurePassword(int length = 12)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
        byte[] data = new byte[length];
        RandomNumberGenerator.Fill(data);

        char[] result = new char[length];
        for (int i = 0; i < length; i++)
        {
            result[i] = chars[data[i] % chars.Length];
        }
        return new string(result);
    }

    void CreateEncryptedPdf()
    {
        string password = GenerateSecurePassword();

        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential PDF</h1><p>Access is restricted.</p>");

        // Set security settings
        pdf.SecuritySettings.UserPassword = password;
        pdf.SecuritySettings.AllowUserAnnotations = false;
        pdf.SecuritySettings.AllowUserCopyPasteContent = false;
        pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint;

        string filePath = "EncryptedSecurePdf.pdf";
        pdf.SaveAs(filePath);

        Console.WriteLine($"Encrypted PDF saved to {filePath} with password: {password}");
    }
}

This method uses the default implementation from the OS's RNG, producing a password suitable for cryptographic operations.

This will apply the generated secure password, so only users with access to the passwords will be able to view the document.

Password-protection pop-up

Additionally, we will be left with an encrypted, secure document with our set permissions:

Encrypted PDF settings

Best Practices and Tips

  • Always use RandomNumberGenerator for anything security-related. Avoid Random for IDs, tokens, or passwords.
  • Keep sensitive data out of logs or user-facing messages, but log enough to track and audit.
  • Consider using timestamps and other metadata alongside random IDs for better traceability.
  • Use IronPDF's built-in security features combined with random keys to protect your documents.
  • Validate random data length and encoding to ensure usability in your context (e.g., filenames, URLs, barcodes).

Summary

Integrating C#'s RandomNumberGenerator class with IronPDF empowers you to generate secure, unique PDFs that meet modern security standards. Whether you're stamping unique IDs, generating encryption keys, or embedding secure tokens, this approach helps you:

  • Prevent document forgery
  • Improve traceability
  • Secure sensitive data within your PDFs

By combining the cryptographic strength of this class with IronPDF's versatile PDF tools, your PDF solutions become safer and more professional.

Try It Yourself!

Ready to boost your PDF security? Try the IronPDF free trial and start experimenting with secure random data today!

Jacob Mellor, Chief Technology Officer @ Team Iron
Chief Technology Officer

Jacob Mellor is Chief Technology Officer at Iron Software and a visionary engineer pioneering C# PDF technology. As the original developer behind Iron Software's core codebase, he has shaped the company's product architecture since its inception, transforming it alongside CEO Cameron Rimington into a 50+ person company serving NASA, Tesla, and global government agencies.

...
Read More

Related Articles

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required