Page 1 of 1

Best way how to get truly random numbers for trading simulations?

Posted: Mon Aug 03, 2026 3:00 pm
by FTtrader
Hi guys, programmers, scalpers,

i would like to discuss topic about how to get trully random numbers for best possible simulations of your forex trading EA or setup.
First of all, common random number generators in classic libraries are not truly random. I don't know, how many peoplet actually that know.
And i think that truly random numbers are elementary basic to be able to test your trading strategy properly.

So here are is my code in Python, how i try to create and store random numbers in operating memory:

Code: Select all

import os
import secrets
import numpy as np

def generate_secure_bytes(size_in_bytes):
    """
    Fills a mutable bytearray in operating memory with highly secure, 
    OS-generated random bytes.
    """
    # os.urandom pulls from the OS hardware entropy pool
    # bytearray ensures it stays in a mutable, efficient memory array
    return bytearray(os.urandom(size_in_bytes))

def generate_secure_integers(count, max_value):
    """
    Generates an array of secure random integers using the secrets module.
    Slower than os.urandom, but easier to use for specific integer ranges.
    """
    return [secrets.randbelow(max_value) for _ in range(count)]

# --- Example Usage ---

# 1. Generate 1 Megabyte of purely random data into memory
megabyte_array = generate_secure_bytes(1024 * 1024)
print(f"Generated {len(megabyte_array)} bytes of secure entropy.")

# 2. If you need a mathematical array (like NumPy) of random floats (0.0 to 1.0)
# We can securely convert random bytes into NumPy floats
def generate_secure_numpy_floats(count):
    # Get random bytes (8 bytes per 64-bit float)
    random_bytes = os.urandom(count * 8)
    # Convert bytes to 64-bit unsigned integers
    random_ints = np.frombuffer(random_bytes, dtype=np.uint64)
    # Divide by the maximum 64-bit integer value to get a float between 0 and 1
    return random_ints / (2**64 - 1)

secure_floats = generate_secure_numpy_floats(100)
print(f"First 3 secure floats: {secure_floats[:3]}")


To achieve the "best randomness possible" in Python, you must avoid the standard random module, which uses a predictable Pseudo-Random Number Generator (PRNG) called the Mersenne Twister.

For true randomness, you have two primary options depending on your strict definition of "true":

OS-Level Hardware Entropy (Cryptographically Secure): Uses your computer's environmental noise (disk reads, keystrokes, CPU thermal noise) gathered by the operating system. This is the standard for high-security cryptographic applications.

Quantum/Atmospheric True Randomness (API): Fetches numbers generated by physical quantum phenomena or atmospheric noise from external scientific servers.

Here is the Python code for both approaches.

Method 1: OS-Level Entropy (Best for Local Memory & Speed)
This approach uses os.urandom() or the secrets module. It pulls directly from your operating system's entropy pool (e.g., /dev/urandom on Linux/Mac, or CryptGenRandom on Windows), which is seeded by actual hardware events.

Re: Best way how to get truly random numbers for trading simulations?

Posted: Mon Aug 03, 2026 3:00 pm
by FTtrader
Method 2: Physical "True" Randomness via API (Strict TRNG)

If you require absolute True Random Number Generation (TRNG) based purely on quantum physics rather than OS hardware interrupts, you must request it from a dedicated hardware generator. The Australian National University (ANU) offers a free API for numbers generated by measuring the quantum fluctuations of the vacuum.

Note: You must install the requests library (pip install requests) to run this.

Code: Select all

import requests
import numpy as np

def get_quantum_random_numbers(count):
    """
    Fetches strictly True Random Numbers generated by quantum vacuum 
    fluctuations from the ANU Quantum Random Number Generator.
    """
    if count > 1024:
        raise ValueError("ANU API allows a maximum of 1024 numbers per request.")
        
    url = f"https://qrng.anu.edu.au/API/jsonI.php?length={count}&type=uint16"
    
    try:
        response = requests.get(url, timeout=10)
        response.raise_for_status()
        data = response.json()
        
        if data.get('success'):
            # Load directly into a numpy operating memory array
            return np.array(data['data'], dtype=np.uint16)
        else:
            raise Exception("API returned an unsuccessful response.")
            
    except requests.RequestException as e:
        print(f"Failed to fetch quantum numbers: {e}")
        return None

# --- Example Usage ---

# Fetch 10 true quantum random integers
quantum_array = get_quantum_random_numbers(10)

if quantum_array is not None:
    print(f"Quantum Random Array: {quantum_array}")
    print(f"Data type in memory: {type(quantum_array)} ({quantum_array.dtype})")

Re: Best way how to get truly random numbers for trading simulations?

Posted: Mon Aug 03, 2026 3:01 pm
by FTtrader
Which should you use?
Use Method 1 (os.urandom) for 99.9% of use cases. It is fast, operates strictly locally, scales instantly to gigabytes of data, and is mathematically secure enough for military-grade cryptography.

Use Method 2 (Quantum API) only if you are running a lottery, conducting strict scientific simulations that demand physical entropy, or are required to completely isolate the randomness source from the host computer's architecture.

Re: Best way how to get truly random numbers for trading simulations?

Posted: Mon Aug 03, 2026 3:02 pm
by FTtrader
And my second favourite programming language is C++,
so i prepare it in it as well:

To achieve true randomness in C++, you must avoid standard pseudo-random number generators (PRNGs) like std::mt19937 (Mersenne Twister) or rand().

C++ offers two excellent ways to generate true hardware-based randomness directly into an operating memory array (like a std::vector or heap-allocated array).

Method 1: Cross-Platform Standard C++ (OS Entropy)
Modern C++ (C++11 and later) includes std::random_device. On almost all modern compilers (GCC, Clang, MSVC), this bypasses software algorithms and directly requests non-deterministic hardware entropy from the operating system (e.g., /dev/urandom on Linux/macOS, or CryptGenRandom on Windows).

This is highly secure and the recommended approach for 99% of use cases.

Code: Select all

#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <chrono>

// Fills an operating memory array with true random unsigned 32-bit integers
std::vector<uint32_t> generate_secure_array(size_t num_elements) {
    // std::random_device requests non-deterministic entropy from the OS
    std::random_device hardware_entropy;
    
    // Allocate the memory array
    std::vector<uint32_t> memory_array(num_elements);
    
    // Fill the array directly using the hardware entropy generator
    std::generate(memory_array.begin(), memory_array.end(), std::ref(hardware_entropy));
    
    return memory_array;
}

int main() {
    size_t array_size = 1000000; // 1 million elements (~4 Megabytes)
    
    std::cout << "Allocating and filling memory array..." << std::endl;
    
    auto start = std::chrono::high_resolution_clock::now();
    std::vector<uint32_t> true_random_array = generate_secure_array(array_size);
    auto end = std::chrono::high_resolution_clock::now();
    
    std::chrono::duration<double> duration = end - start;
    
    std::cout << "Filled " << true_random_array.size() << " integers in " 
              << duration.count() << " seconds." << std::endl;
              
    std::cout << "First 3 true random numbers: " << '\n'
              << true_random_array[0] << '\n'
              << true_random_array[1] << '\n'
              << true_random_array[2] << std::endl;

    return 0;
}

Re: Best way how to get truly random numbers for trading simulations?

Posted: Mon Aug 03, 2026 3:03 pm
by FTtrader
Method 2: Direct CPU Hardware Instruction (Bare Metal TRNG)
If you want the absolute purest form of randomness without trusting the operating system's entropy pool wrapper, you can ask your CPU processor directly. Modern Intel and AMD processors have an on-die thermal noise generator.

You can access this using the _rdrand64_step compiler intrinsic. This queries the physical silicon for a true random number generated by quantum/thermal fluctuations inside the CPU.

Note: This only works on x86_64 architectures (Intel/AMD).

Code: Select all

#include <iostream>
#include <vector>
#include <immintrin.h> // Required for hardware intrinsics (RDRAND)

std::vector<uint64_t> generate_cpu_hardware_entropy(size_t num_elements) {
    std::vector<uint64_t> memory_array;
    memory_array.reserve(num_elements);

    for (size_t i = 0; i < num_elements; ++i) {
        unsigned long long random_value;
        
        // _rdrand64_step directly queries the CPU's hardware random number generator.
        // It returns 1 if successful, and 0 if the hardware entropy pool is temporarily exhausted.
        int success = 0;
        int retries = 10; // Prevent infinite loop if hardware fails
        
        while (success == 0 && retries > 0) {
            success = _rdrand64_step(&random_value);
            retries--;
        }
        
        if (success == 1) {
            memory_array.push_back(random_value);
        } else {
            std::cerr << "CRITICAL: CPU Hardware entropy pool exhausted!" << std::endl;
            break;
        }
    }
    
    return memory_array;
}

int main() {
    size_t array_size = 10;
    
    std::vector<uint64_t> bare_metal_array = generate_cpu_hardware_entropy(array_size);
    
    std::cout << "Direct CPU Silicon Random Numbers (64-bit):" << std::endl;
    for (size_t i = 0; i < bare_metal_array.size(); ++i) {
        std::cout << bare_metal_array[i] << std::endl;
    }

    return 0;
}

Re: Best way how to get truly random numbers for trading simulations?

Posted: Mon Aug 03, 2026 3:03 pm
by FTtrader
Which one should you choose?

Use Method 1 (std::random_device) for almost everything. It is standard, cross-platform (works on ARM devices like Apple Silicon or Raspberry Pi), and inherently mixes multiple environmental noise sources (disk writes, network timings, etc.) via the OS kernel.

Use Method 2 (_rdrand64_step) only if you are building an ultra-low-latency application on x86 hardware, writing a custom operating system, or explicitly need to avoid operating system API calls for security or performance reasons.

Re: Best way how to get truly random numbers for trading simulations?

Posted: Mon Aug 03, 2026 3:05 pm
by FTtrader
And last one is in C, which was my favorite programming language before study on University:

To achieve true randomness in C, you must completely avoid the standard library's rand() and srand() functions, which rely on highly predictable mathematical formulas (usually a Linear Congruential Generator).

Because C does not have a built-in cryptographic randomness standard like C++ (std::random_device) or Python (secrets), you must interact directly with the Operating System's cryptographic APIs or the CPU hardware.

Here are the two best methods for generating true randomness directly into a heap-allocated memory array in C.

Method 1: Cross-Platform OS Entropy (Recommended)
This is the most robust and secure approach. It asks the operating system's kernel to provide non-deterministic entropy gathered from hardware events.

To make this work across different systems, we use compiler directives (#ifdef _WIN32) to call Windows' modern Cryptography Next Generation (CNG) API or POSIX's /dev/urandom for Linux, macOS, and BSD.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// Platform-specific headers
#ifdef _WIN32
    #include <windows.h>
    #include <bcrypt.h>
    #pragma comment(lib, "bcrypt.lib") // Required for MSVC linker
#else
    #include <fcntl.h>
    #include <unistd.h>
#endif

/*
 * Allocates and fills a memory array with cryptographically secure random integers.
 * Returns 0 on success, -1 on failure.
 */
int fill_secure_random_array(uint32_t *array, size_t num_elements) {
    size_t bytes_to_read = num_elements * sizeof(uint32_t);

#ifdef _WIN32
    // Windows: Use the modern CNG API (System Preferred RNG)
    NTSTATUS status = BCryptGenRandom(NULL, (PUCHAR)array, (ULONG)bytes_to_read, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
    if (status < 0) {
        return -1;
    }
#else
    // POSIX (Linux/macOS): Read directly from the OS hardware entropy pool
    int fd = open("/dev/urandom", O_RDONLY);
    if (fd < 0) {
        return -1;
    }
    
    size_t total_read = 0;
    while (total_read < bytes_to_read) {
        ssize_t result = read(fd, ((char*)array) + total_read, bytes_to_read - total_read);
        if (result < 0) {
            close(fd);
            return -1;
        }
        total_read += result;
    }
    close(fd);
#endif

    return 0;
}

int main() {
    size_t array_size = 1000000; // 1 million elements
    
    // Allocate the operating memory array on the heap
    uint32_t *secure_array = (uint32_t *)malloc(array_size * sizeof(uint32_t));
    if (secure_array == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    printf("Filling memory array with OS-level entropy...\n");
    
    if (fill_secure_random_array(secure_array, array_size) == 0) {
        printf("Successfully generated %zu secure random integers.\n", array_size);
        printf("First 3 numbers:\n%u\n%u\n%u\n", 
               secure_array[0], secure_array[1], secure_array[2]);
    } else {
        printf("Failed to generate random numbers.\n");
    }

    // Always free heap memory in C
    free(secure_array);
    return 0;
}

Re: Best way how to get truly random numbers for trading simulations?

Posted: Mon Aug 03, 2026 3:06 pm
by FTtrader
Method 2: Direct CPU Hardware Instruction (Bare Metal x86_64)

If you are writing low-level C code (like a driver or embedded system on an x86 architecture) and want to bypass the OS entirely, you can query the CPU's internal thermal noise generator directly using the _rdrand64_step intrinsic.

Note: This strictly requires an Intel or AMD processor.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <immintrin.h> // Header for x86 hardware intrinsics

/*
 * Fills an array using direct CPU silicon quantum/thermal fluctuations.
 * Returns 0 on success, -1 on hardware exhaustion.
 */
int fill_hardware_random_array(uint64_t *array, size_t num_elements) {
    for (size_t i = 0; i < num_elements; i++) {
        unsigned long long rand_val;
        int success = 0;
        int retries = 10; // The hardware pool can temporarily empty out
        
        // Query the CPU directly
        while (success == 0 && retries > 0) {
            success = _rdrand64_step(&rand_val);
            retries--;
        }
        
        if (success == 1) {
            array[i] = (uint64_t)rand_val;
        } else {
            // If the hardware entropy pool is exhausted and doesn't recover
            return -1; 
        }
    }
    return 0;
}

int main() {
    size_t array_size = 5;
    
    // Allocate memory array
    uint64_t *hardware_array = (uint64_t *)malloc(array_size * sizeof(uint64_t));
    if (hardware_array == NULL) return 1;

    printf("Querying CPU hardware for pure entropy...\n");
    
    if (fill_hardware_random_array(hardware_array, array_size) == 0) {
        for (size_t i = 0; i < array_size; i++) {
            printf("Hardware TRNG [%zu]: %llu\n", i, (unsigned long long)hardware_array[i]);
        }
    } else {
        printf("CRITICAL: CPU hardware entropy generator failed.\n");
    }

    free(hardware_array);
    return 0;
}
Compilation Notes for C
Linux/macOS (Method 1 & 2): Standard compilation gcc main.c -o true_rng (For Method 2, you may need to add -mrdrnd to enable hardware instructions: gcc main.c -mrdrnd -o true_rng).

Windows (Method 1): If compiling manually from the command line, you must link the bcrypt library: cl main.c bcrypt.lib.

If you will like it or do you have any extension of this let me know,
have a nice day.

Re: Best way how to get truly random numbers for trading simulations?

Posted: Mon Aug 17, 2026 6:29 pm
by HansFX
FTtrader wrote: Mon Aug 03, 2026 3:00 pm Hi guys, programmers, scalpers,

i would like to discuss topic about how to get trully random numbers for best possible simulations of your forex trading EA or setup.
First of all, common random number generators in classic libraries are not truly random. I don't know, how many peoplet actually that know.
And i think that truly random numbers are elementary basic to be able to test your trading strategy properly.

So here are is my code in Python, how i try to create and store random numbers in operating memory:

Code: Select all

import os
import secrets
import numpy as np

def generate_secure_bytes(size_in_bytes):
    """
    Fills a mutable bytearray in operating memory with highly secure, 
    OS-generated random bytes.
    """
    # os.urandom pulls from the OS hardware entropy pool
    # bytearray ensures it stays in a mutable, efficient memory array
    return bytearray(os.urandom(size_in_bytes))

def generate_secure_integers(count, max_value):
    """
    Generates an array of secure random integers using the secrets module.
    Slower than os.urandom, but easier to use for specific integer ranges.
    """
    return [secrets.randbelow(max_value) for _ in range(count)]

# --- Example Usage ---

# 1. Generate 1 Megabyte of purely random data into memory
megabyte_array = generate_secure_bytes(1024 * 1024)
print(f"Generated {len(megabyte_array)} bytes of secure entropy.")

# 2. If you need a mathematical array (like NumPy) of random floats (0.0 to 1.0)
# We can securely convert random bytes into NumPy floats
def generate_secure_numpy_floats(count):
    # Get random bytes (8 bytes per 64-bit float)
    random_bytes = os.urandom(count * 8)
    # Convert bytes to 64-bit unsigned integers
    random_ints = np.frombuffer(random_bytes, dtype=np.uint64)
    # Divide by the maximum 64-bit integer value to get a float between 0 and 1
    return random_ints / (2**64 - 1)

secure_floats = generate_secure_numpy_floats(100)
print(f"First 3 secure floats: {secure_floats[:3]}")


To achieve the "best randomness possible" in Python, you must avoid the standard random module, which uses a predictable Pseudo-Random Number Generator (PRNG) called the Mersenne Twister.

For true randomness, you have two primary options depending on your strict definition of "true":

OS-Level Hardware Entropy (Cryptographically Secure): Uses your computer's environmental noise (disk reads, keystrokes, CPU thermal noise) gathered by the operating system. This is the standard for high-security cryptographic applications.

Quantum/Atmospheric True Randomness (API): Fetches numbers generated by physical quantum phenomena or atmospheric noise from external scientific servers.

Here is the Python code for both approaches.

Method 1: OS-Level Entropy (Best for Local Memory & Speed)
This approach uses os.urandom() or the secrets module. It pulls directly from your operating system's entropy pool (e.g., /dev/urandom on Linux/Mac, or CryptGenRandom on Windows), which is seeded by actual hardware events.
Hello.

I must be honest with you—I am not a programmer. I do not know what this "Python" is, and I do not understand your "code." My brain is for the charts, for the price movement, and for the discipline of the trade. I am an old man; I prefer to look at the candles, not the computer's hidden logic.

But, I am interested. I see you talking about "truly random numbers" for the "simulations."

I do not understand the math, but I understand the market. In my many years, I have seen that the market is very... unpredictable. Sometimes it looks like it follows a pattern, and other times it behaves like a wild animal.

My question to you is this: Why does the "randomness" of the computer matter for my trade?

If I am running an EA (an automated system), and your "random numbers" are better, does it mean the simulation is more like the real world? If the computer "knows" the numbers too well, does it make the trade look better on paper than it will actually be when I am trading in the London session?

Can you explain to me, in simple words—not for a programmer, but for an old trader—why this matters for the "quality" of the trade? Does a "truly random" number make the test of my strategy more honest?

I want to understand the "why," even if I cannot understand the "how" of your code.

Regards,

Hans

Re: Best way how to get truly random numbers for trading simulations?

Posted: Mon Aug 17, 2026 8:35 pm
by FTtrader
HansFX wrote: Mon Aug 17, 2026 6:29 pm
FTtrader wrote: Mon Aug 03, 2026 3:00 pm Hi guys, programmers, scalpers,

i would like to discuss topic about how to get trully random numbers for best possible simulations of your forex trading EA or setup.
First of all, common random number generators in classic libraries are not truly random. I don't know, how many peoplet actually that know.
And i think that truly random numbers are elementary basic to be able to test your trading strategy properly.

So here are is my code in Python, how i try to create and store random numbers in operating memory:

Code: Select all

import os
import secrets
import numpy as np

def generate_secure_bytes(size_in_bytes):
    """
    Fills a mutable bytearray in operating memory with highly secure, 
    OS-generated random bytes.
    """
    # os.urandom pulls from the OS hardware entropy pool
    # bytearray ensures it stays in a mutable, efficient memory array
    return bytearray(os.urandom(size_in_bytes))

def generate_secure_integers(count, max_value):
    """
    Generates an array of secure random integers using the secrets module.
    Slower than os.urandom, but easier to use for specific integer ranges.
    """
    return [secrets.randbelow(max_value) for _ in range(count)]

# --- Example Usage ---

# 1. Generate 1 Megabyte of purely random data into memory
megabyte_array = generate_secure_bytes(1024 * 1024)
print(f"Generated {len(megabyte_array)} bytes of secure entropy.")

# 2. If you need a mathematical array (like NumPy) of random floats (0.0 to 1.0)
# We can securely convert random bytes into NumPy floats
def generate_secure_numpy_floats(count):
    # Get random bytes (8 bytes per 64-bit float)
    random_bytes = os.urandom(count * 8)
    # Convert bytes to 64-bit unsigned integers
    random_ints = np.frombuffer(random_bytes, dtype=np.uint64)
    # Divide by the maximum 64-bit integer value to get a float between 0 and 1
    return random_ints / (2**64 - 1)

secure_floats = generate_secure_numpy_floats(100)
print(f"First 3 secure floats: {secure_floats[:3]}")


To achieve the "best randomness possible" in Python, you must avoid the standard random module, which uses a predictable Pseudo-Random Number Generator (PRNG) called the Mersenne Twister.

For true randomness, you have two primary options depending on your strict definition of "true":

OS-Level Hardware Entropy (Cryptographically Secure): Uses your computer's environmental noise (disk reads, keystrokes, CPU thermal noise) gathered by the operating system. This is the standard for high-security cryptographic applications.

Quantum/Atmospheric True Randomness (API): Fetches numbers generated by physical quantum phenomena or atmospheric noise from external scientific servers.

Here is the Python code for both approaches.

Method 1: OS-Level Entropy (Best for Local Memory & Speed)
This approach uses os.urandom() or the secrets module. It pulls directly from your operating system's entropy pool (e.g., /dev/urandom on Linux/Mac, or CryptGenRandom on Windows), which is seeded by actual hardware events.
Hello.

I must be honest with you—I am not a programmer. I do not know what this "Python" is, and I do not understand your "code." My brain is for the charts, for the price movement, and for the discipline of the trade. I am an old man; I prefer to look at the candles, not the computer's hidden logic.

But, I am interested. I see you talking about "truly random numbers" for the "simulations."

I do not understand the math, but I understand the market. In my many years, I have seen that the market is very... unpredictable. Sometimes it looks like it follows a pattern, and other times it behaves like a wild animal.

My question to you is this: Why does the "randomness" of the computer matter for my trade?

If I am running an EA (an automated system), and your "random numbers" are better, does it mean the simulation is more like the real world? If the computer "knows" the numbers too well, does it make the trade look better on paper than it will actually be when I am trading in the London session?

Can you explain to me, in simple words—not for a programmer, but for an old trader—why this matters for the "quality" of the trade? Does a "truly random" number make the test of my strategy more honest?

I want to understand the "why," even if I cannot understand the "how" of your code.

Regards,

Hans
I got it Hans,

i will try to think about it how to explain corectly :-)
But don't worry, will find the way.