### Random Number Generator Setup Source: https://context7.com/cran/randtoolbox/llms.txt Demonstrates how to set up and configure different pseudo-random number generators, including Mersenne Twister and congruRand, and how to save and restore generator states. ```APIDOC ## Random Number Generator Setup ### Description This section illustrates how to initialize and configure various pseudo-random number generators provided by the `randtoolbox` package. It covers setting parameters for generators like Mersenne Twister and congruRand, and demonstrates the functionality to save and restore the state of a generator. ### Mersenne Twister Example ```r # For Mersenne Twister set.generator("MersenneTwister", initialization = "init2002", resolution = "32", seed = 12345) desc_mt <- get.description() print(desc_mt$name) print(desc_mt$parameters) print(desc_mt$authors) ``` ### congruRand Example ```r # For congruRand set.generator("congruRand", mod = 2^31 - 1, mult = 16807, incr = 0, seed = 100) desc_cg <- get.description() print(desc_cg$name) print(desc_cg$parameters) print(desc_cg$authors) ``` ### Save and Restore Generator State Example ```r # Save and restore generator state set.generator("WELL", order = 512, seed = 42) state_before <- get.description() x1 <- runif(100) # Restore state put.description(state_before) x2 <- runif(100) print(identical(x1, x2)) ``` ``` -------------------------------- ### Install randtoolbox Package Source: https://github.com/cran/randtoolbox/blob/master/README.md Installs the randtoolbox package from CRAN. This is a prerequisite for using the package's functionalities. ```r install.packages("randtoolbox") ``` -------------------------------- ### Generate Halton Sequences with randtoolbox Source: https://context7.com/cran/randtoolbox/llms.txt Generates Halton low-discrepancy sequences, which are useful for quasi-Monte Carlo integration due to their excellent space-filling properties. Supports various transformations and starting points. ```r library(randtoolbox) # Normal deviates transformation x_normal <- halton(n = 500, dim = 3, normal = TRUE) print(summary(x_normal[,1])) # Sequence continuation first_part <- halton(5) second_part <- halton(5, init = FALSE) full_sequence <- halton(10) print(identical(full_sequence, c(first_part, second_part))) # Start from machine time for randomization x_random_start <- halton(10, usetime = TRUE) # Mixed with SFMT x_mixed <- halton(100, mixed = TRUE) # Start from position 0 (recommended) x_from_zero <- halton(10, start = 0) # Visualization of 2D low-discrepancy par(mfrow = c(1, 2)) # Halton sequence - good space filling plot(halton(500, dim = 2), pch = 19, cex = 0.5, main = "Halton Sequence", xlab = "x", ylab = "y") # Compare to pseudo-random plot(matrix(runif(1000), ncol = 2), pch = 19, cex = 0.5, main = "Pseudo-random", xlab = "x", ylab = "y") # Quality test ks_result <- ks.test(halton(10000), punif) print(ks_result$statistic) ``` -------------------------------- ### Torus Quasi-Random Number Generator (R) Source: https://context7.com/cran/randtoolbox/llms.txt Generates quasi-random low-discrepancy sequences using the Torus algorithm. Supports multi-dimensional sequences, sequence continuation, custom prime bases, randomization mixing, normal transformation, and starting from a specific position. Excellent for quasi-Monte Carlo methods. ```R library(randtoolbox) # Basic Torus sequence x <- torus(100) print(x[1:10]) # Multi-dimensional low-discrepancy sequence x <- torus(n = 100, dim = 5) print(dim(x)) print(x[1:3, ]) # Sequence continuation torus(5) torus(5, init = FALSE) # Combined call equals separate calls combined <- torus(10) print(identical(combined, c(torus(5), torus(5, init = FALSE)))) # Custom prime numbers x_custom <- torus(10, prime = c(2, 3, 5, 7, 11)) print(dim(x_custom)) # Mixed with SFMT for randomization x_mixed <- torus(100, mixed = TRUE) # Normal transformation x_normal <- torus(1000, normal = TRUE) print(summary(x_normal)) # Start from position 0 x_from_zero <- torus(10, start = 0) # Excellent uniformity for quasi-Monte Carlo ks_result <- ks.test(torus(10000), punif) print(ks_result$statistic) ``` -------------------------------- ### Configure and Manage Random Number Generators in R Source: https://context7.com/cran/randtoolbox/llms.txt Demonstrates how to initialize different random number generators such as Mersenne Twister and congruRand, retrieve their metadata, and save/restore the generator state to ensure reproducibility. ```R set.generator("MersenneTwister", initialization = "init2002", resolution = "32", seed = 12345) desc_mt <- get.description() print(desc_mt$name) set.generator("congruRand", mod = 2^31 - 1, mult = 16807, incr = 0, seed = 100) desc_cg <- get.description() set.generator("WELL", order = 512, seed = 42) state_before <- get.description() x1 <- runif(100) put.description(state_before) x2 <- runif(100) print(identical(x1, x2)) ``` -------------------------------- ### Generate Numbers with SIMD-oriented Fast Mersenne Twister Source: https://context7.com/cran/randtoolbox/llms.txt Explains how to use the SFMT generator, including configuring Mersenne exponents, handling parameter sets to reduce stream correlation, and creating hybrid sequences with the Torus generator. ```r library(randtoolbox) # Basic usage with default Mersenne exponent (19937) x <- SFMT(1000) print(summary(x)) # Different Mersenne exponents for varying periods setSeed(42) x607 <- SFMT(10, mexp = 607, usepset = FALSE) # Use different parameter sets to avoid correlation stream1 <- SFMT(100, mexp = 607) stream2 <- SFMT(100, mexp = 607) print(cor(stream1, stream2)) # Multi-dimensional output setSeed(123) matrix_out <- SFMT(n = 500, dim = 5) print(dim(matrix_out)) # Hybrid with Torus sequence (1/3 from Torus appended) hybrid <- SFMT(1000, withtorus = 1/3) print(length(hybrid)) ``` -------------------------------- ### Access randtoolbox Documentation Source: https://github.com/cran/randtoolbox/blob/master/README.md Opens the main help documentation for the randtoolbox package in R, providing an overview of its features and usage. ```r help(randtoolbox) ``` -------------------------------- ### Utility Functions Source: https://context7.com/cran/randtoolbox/llms.txt Documentation for utility functions including Stirling numbers of the second kind and permutation generation. ```APIDOC ## Utility Functions ### stirling - Stirling Numbers of the Second Kind #### Description Computes Stirling numbers of the second kind S(n,k), which count the number of ways to partition n elements into k non-empty subsets. Used internally by collision tests. #### Parameters - **n** (numeric) - The total number of elements. #### Request Example ```r library(randtoolbox) # Stirling numbers for n=4 s4 <- stirling(4) print(s4) # [1] 0 1 7 6 1 # Stirling numbers for n=5 s5 <- stirling(5) print(s5) # [1] 0 1 15 25 10 1 # n=0 case print(stirling(0)) # [1] 1 # n=1 case print(stirling(1)) # [1] 0 1 # Bell numbers are sums of Stirling numbers bell_5 <- sum(stirling(5)) print(bell_5) # [1] 52 ``` ### permut - Generate All Permutations #### Description Generates all permutations of integers from 1 to n. Used internally by order test but available for general use. #### Parameters - **n** (numeric) - The upper limit for the integers to permute (i.e., generates permutations of 1 to n). #### Request Example ```r library(randtoolbox) # All permutations of 1,2,3 p3 <- permut(3) print(p3) # [,1] [,2] [,3] # [1,] 3 1 2 # [2,] 3 2 1 # [3,] 1 3 2 # [4,] 2 3 1 # [5,] 1 2 3 # [6,] 2 1 3 print(nrow(p3)) # [1] 6 # Permutations of 1,2 p2 <- permut(2) print(p2) # [,1] [,2] # [1,] 1 2 # [2,] 2 1 # For n=4 (24 permutations) p4 <- permut(4) print(nrow(p4)) # [1] 24 # Warning: large n is very slow # permut(10) has 3,628,800 rows ``` ``` -------------------------------- ### Generate Sobol Sequences with randtoolbox Source: https://context7.com/cran/randtoolbox/llms.txt Generates Sobol low-discrepancy sequences, known for their uniformity and suitability for quasi-Monte Carlo integration. Supports high dimensions and normal transformations. ```r library(randtoolbox) # Basic Sobol sequence x <- sobol(n = 10, dim = 1) print(x) # Multi-dimensional Sobol sequence x <- sobol(n = 10, dim = 5) print(x[1:5, ]) # Normal transformation for multivariate normal simulation x_normal <- sobol(n = 1000, dim = 5, normal = TRUE) print(apply(x_normal, 2, mean)) # Should be near 0 print(apply(x_normal, 2, sd)) # Should be near 1 # Sequence continuation first_part <- sobol(5) second_part <- sobol(5, init = FALSE) full_sequence <- sobol(10) print(identical(full_sequence, c(first_part, second_part))) # Mixed with SFMT for randomization x_mixed <- sobol(100, mixed = TRUE) # High-dimensional Sobol (up to 1111 dimensions) x_high_dim <- sobol(n = 100, dim = 100) print(dim(x_high_dim)) # Start from position 0 (recommended by Owen 2020) x_from_zero <- sobol(10, start = 0) # Quasi-Monte Carlo integration example # Estimate integral of x^2 from 0 to 1 (true value = 1/3) n <- 10000 x_sobol <- sobol(n) x_random <- runif(n) print(mean(x_sobol^2)) # QMC estimate print(mean(x_random^2)) # MC estimate # Visualization comparing distributions par(mfrow = c(2, 2)) hist(sobol(500, 1), main = "Uniform Sobol", xlab = "x", col = "steelblue3", border = "white") hist(sobol(500, 1, normal = TRUE), main = "Normal Sobol", xlab = "x", col = "steelblue3", border = "white") hist(runif(500), main = "Uniform Random", xlab = "x", col = "coral", border = "white") hist(rnorm(500), main = "Normal Random", xlab = "x", col = "coral", border = "white") ``` -------------------------------- ### Load randtoolbox Package Source: https://github.com/cran/randtoolbox/blob/master/README.md Loads the randtoolbox package into the current R session, making its functions available for use. ```r library(randtoolbox) ``` -------------------------------- ### Set Random Seed for randtoolbox Generators Source: https://context7.com/cran/randtoolbox/llms.txt Demonstrates how to initialize the randtoolbox random number generators using setSeed. This function ensures reproducibility across different generator types like SFMT, WELL, and congruRand. ```r library(randtoolbox) # Set seed using a memorable date (e.g., birthday: day/month/year) setSeed(15061985) # Generate reproducible sequence x1 <- SFMT(5, usepset = FALSE) print(x1) # Reset seed and regenerate same sequence setSeed(15061985) x2 <- SFMT(5, usepset = FALSE) print(identical(x1, x2)) # Works across all randtoolbox generators setSeed(1302) print(congruRand(3)) setSeed(1302) print(WELL(3)) ``` -------------------------------- ### SFMT - SIMD-oriented Fast Mersenne Twister Source: https://context7.com/cran/randtoolbox/llms.txt Generates pseudo-random numbers using the SIMD-oriented Fast Mersenne Twister algorithm. Supports various Mersenne exponents and parameter sets. ```APIDOC ## SFMT - SIMD-oriented Fast Mersenne Twister ### Description Generates pseudo-random numbers using the SIMD-oriented Fast Mersenne Twister algorithm with configurable Mersenne exponents (period 2^mexp - 1). Supports multiple parameter sets to avoid correlation between streams. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters * **n** (numeric) - Required - The number of random numbers to generate. * **mexp** (numeric) - Optional - The Mersenne exponent. Available values: 607, 1279, 2281, 4253, 11213, 19937, 44497, 86243, 132049, 216091. Defaults to 19937. * **usepset** (logical) - Optional - If FALSE, uses a fixed parameter set for reproducibility. Defaults to TRUE. * **dim** (numeric) - Optional - The dimension of the output array (default is 1 for a vector). * **seed** (numeric) - Optional - Seed for the generator. If not provided, uses the current seed set by `setSeed()`. * **withtorus** (numeric) - Optional - A value between 0 and 1 indicating the proportion of numbers to be drawn from the Torus quasi-random sequence and appended to the SFMT sequence. ### Request Example ```R library(randtoolbox) # Basic usage with default Mersenne exponent (19937) x <- SFMT(1000) print(summary(x)) # Different Mersenne exponents for varying periods setSeed(42) x607 <- SFMT(10, mexp = 607, usepset = FALSE) print(x607[1:5]) # Use different parameter sets (default) to avoid correlation between runs stream1 <- SFMT(100, mexp = 607) stream2 <- SFMT(100, mexp = 607) print(cor(stream1, stream2)) # Fixed parameter set for reproducibility setSeed(08082008) x1 <- SFMT(5, usepset = FALSE) setSeed(08082008) x2 <- SFMT(5, usepset = FALSE) print(identical(x1, x2)) # Multi-dimensional output setSeed(123) matrix_out <- SFMT(n = 500, dim = 5) print(dim(matrix_out)) # Hybrid with Torus sequence (1/3 from Torus appended) hybrid <- SFMT(1000, withtorus = 1/3) print(length(hybrid)) ``` ### Response * **random_numbers** (numeric vector or matrix) - The generated pseudo-random numbers. ### Response Example ```R # Example for basic usage summary: # Min. 1st Qu. Median Mean 3rd Qu. Max. # 0.00023 0.25123 0.50234 0.50012 0.75345 0.99978 # Example for first 5 numbers with mexp=607: # [1] 0.3456789 0.7891234 0.1234567 0.5678901 0.9012345 # Example for correlation between streams: # [1] 0.0234567 # Example for identical sequences: # [1] TRUE # Example for dimension of multi-dimensional output: # [1] 500 5 # Example for length of hybrid sequence: # [1] 1000 ``` ``` -------------------------------- ### Generate Numbers with Linear Congruential Generator Source: https://context7.com/cran/randtoolbox/llms.txt Shows the usage of congruRand to generate pseudo-random numbers. It covers default Park-Miller parameters, custom generator parameters, and multi-dimensional output generation. ```r library(randtoolbox) # Default Park-Miller sequence setSeed(1) x <- congruRand(10) print(x) # Multi-dimensional output setSeed(42) matrix_output <- congruRand(n = 100, dim = 3) print(dim(matrix_output)) # Custom parameters (Knuth-Lewis generator) setSeed(12345) x <- congruRand(n = 1000, mod = 2^32, mult = 1664525, incr = 1013904223) print(summary(x)) # Kolmogorov-Smirnov test for uniformity ks_result <- ks.test(congruRand(10000), punif) print(ks_result$p.value) ``` -------------------------------- ### Sobol Sequence Generation Source: https://context7.com/cran/randtoolbox/llms.txt Generates Sobol low-discrepancy sequences, known for their excellent uniformity and suitability for quasi-Monte Carlo integration. Supports high dimensions and optional scrambling. ```APIDOC ## sobol - Sobol Sequence ### Description Generates Sobol low-discrepancy sequences with optional scrambling. Supports up to 1111 dimensions and provides excellent uniformity for quasi-Monte Carlo integration. ### Method `sobol(n, dim = 1, normal = FALSE, mixed = FALSE, start = 0)` ### Parameters #### Path Parameters - **n** (integer) - Required - The number of points to generate. - **dim** (integer) - Optional - The dimension of the sequence. Defaults to 1. - **normal** (boolean) - Optional - If TRUE, transforms the sequence to follow a normal distribution. Defaults to FALSE. - **mixed** (boolean) - Optional - If TRUE, mixes the sequence with SFMT for randomization. Defaults to FALSE. - **start** (integer) - Optional - The starting index for the sequence. Defaults to 0. ### Request Example ```r library(randtoolbox) # Generate a 5-dimensional Sobol sequence x <- sobol(n = 10, dim = 5) print(head(x[, 1:3])) ``` ### Response #### Success Response (200) - **x** (matrix or vector) - The generated Sobol sequence. #### Response Example ```r # Example output for sobol(n = 5, dim = 3) # [,1] [,2] [,3] # [1,] 0.5000 0.5000 0.5000 # [2,] 0.7500 0.2500 0.7500 # [3,] 0.2500 0.7500 0.2500 # [4,] 0.3750 0.3750 0.6250 # [5,] 0.8750 0.8750 0.1250 ``` ``` -------------------------------- ### Halton Sequence Generation Source: https://context7.com/cran/randtoolbox/llms.txt Generates Halton low-discrepancy sequences, which are useful for quasi-Monte Carlo integration due to their excellent space-filling properties. Supports various options like normal transformation, sequence continuation, and randomization. ```APIDOC ## halton - Halton Sequence ### Description Generates Halton low-discrepancy sequences. These sequences are known for their good space-filling properties, making them suitable for quasi-Monte Carlo integration. ### Method `halton(n, dim = 1, normal = FALSE, usetime = FALSE, mixed = FALSE, start = 0)` ### Parameters #### Path Parameters - **n** (integer) - Required - The number of points to generate. - **dim** (integer) - Optional - The dimension of the sequence. Defaults to 1. - **normal** (boolean) - Optional - If TRUE, transforms the sequence to follow a normal distribution. Defaults to FALSE. - **usetime** (boolean) - Optional - If TRUE, uses the system time for randomization. Defaults to FALSE. - **mixed** (boolean) - Optional - If TRUE, mixes the sequence with SFMT for randomization. Defaults to FALSE. - **start** (integer) - Optional - The starting index for the sequence. Defaults to 0. ### Request Example ```r library(randtoolbox) # Generate a 2D Halton sequence x <- halton(n = 100, dim = 2) print(head(x)) ``` ### Response #### Success Response (200) - **x** (matrix or vector) - The generated Halton sequence. #### Response Example ```r # Example output for halton(n = 5, dim = 2) # [,1] [,2] # [1,] 0.5000000 0.3333333 # [2,] 0.2500000 0.6666667 # [3,] 0.7500000 0.1111111 # [4,] 0.1250000 0.4444444 # [5,] 0.6250000 0.7777778 ``` ``` -------------------------------- ### Generate Permutations of Integers in R Source: https://context7.com/cran/randtoolbox/llms.txt Generates all possible permutations of integers from 1 to n. This utility is used for order tests and general combinatorial tasks, though it is computationally expensive for large n. ```R library(randtoolbox) p3 <- permut(3) print(p3) print(nrow(p3)) ``` -------------------------------- ### Retrieve Prime Numbers with get.primes Source: https://context7.com/cran/randtoolbox/llms.txt Retrieves the first n prime numbers from an internal table of 100,000 primes. This function is used internally by some quasi-random generators but can also be used for general purposes. ```r library(randtoolbox) # Get first 10 prime numbers primes <- get.primes(10) print(primes) # Get first 100 primes primes100 <- get.primes(100) print(primes100[90:100]) # Maximum available: 100,000 primes primes_max <- get.primes(100000) print(tail(primes_max, 5)) # Use custom primes in Torus algorithm my_primes <- get.primes(5) x <- torus(10, prime = my_primes) print(dim(x)) ``` -------------------------------- ### congruRand - Linear Congruential Generator Source: https://context7.com/cran/randtoolbox/llms.txt Generates pseudo-random numbers using the linear congruential algorithm. It supports custom parameters and multi-dimensional output. ```APIDOC ## congruRand - Linear Congruential Generator ### Description Generates pseudo-random numbers using the linear congruential algorithm: u_k = [(a * u_{k-1} + c) mod m] / m. Default parameters implement the Park-Miller sequence (a=16807, m=2^31-1, c=0). ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters * **n** (numeric) - Required - The number of random numbers to generate. * **mod** (numeric) - Optional - The modulus (m). * **mult** (numeric) - Optional - The multiplier (a). * **incr** (numeric) - Optional - The increment (c). * **dim** (numeric) - Optional - The dimension of the output array (default is 1 for a vector). * **echo** (logical) - Optional - If TRUE, prints the sequence of seeds used during generation. * **seed** (numeric) - Optional - Seed for the generator. If not provided, uses the current seed set by `setSeed()`. ### Request Example ```R library(randtoolbox) # Default Park-Miller sequence setSeed(1) x <- congruRand(10) print(x) # Multi-dimensional output setSeed(42) matrix_output <- congruRand(n = 100, dim = 3) print(dim(matrix_output)) # Custom parameters (Knuth-Lewis generator) setSeed(12345) x <- congruRand(n = 1000, mod = 2^32, mult = 1664525, incr = 1013904223) print(summary(x)) ``` ### Response * **random_numbers** (numeric vector or matrix) - The generated pseudo-random numbers. ### Response Example ```R # Example for basic usage: # [1] 7.826369e-06 1.315378e-01 7.556053e-01 4.586501e-01 ... # Example for multi-dimensional output: # [1] 100 3 # Example for summary statistics: # Min. 1st Qu. Median Mean 3rd Qu. Max. # 0.00012 0.24567 0.49234 0.50012 0.75678 0.99987 ``` ``` -------------------------------- ### WELL Pseudo-Random Number Generator (R) Source: https://context7.com/cran/randtoolbox/llms.txt Generates pseudo-random numbers using WELL generators. Supports various orders (e.g., 512, 1024, 19937, 44497) and optional tempering for improved quality. Can produce multi-dimensional output and uses a dedicated seeding function for reproducibility. ```R library(randtoolbox) library(rngWELL) # Basic WELL512 generator setSeed(42) x <- WELL(1000, order = 512) print(summary(x)) # Different orders available x512 <- WELL(10, order = 512) x1024 <- WELL(10, order = 1024) x19937 <- WELL(10, order = 19937) x44497 <- WELL(10, order = 44497) # Enable tempering for better quality setSeed(08082008) x_no_temper <- WELL(10, order = 19937, temper = FALSE) setSeed(08082008) x_tempered <- WELL(10, order = 19937, temper = TRUE) print(x_no_temper[1:5]) print(x_tempered[1:5]) # Version 'a' or 'b' x_a <- WELL(10, order = 19937, version = "a") x_b <- WELL(10, order = 19937, version = "b") # Multi-dimensional output setSeed(123) samples <- WELL(n = 1000, dim = 3, order = 19937, temper = TRUE) print(dim(samples)) # Use dedicated WELL seed function setSeed4WELL(08082008) x1 <- WELL(5, order = 19937) setSeed4WELL(08082008) x2 <- WELL(5, order = 19937) print(identical(x1, x2)) # Quality comparison ks_512 <- ks.test(WELL(10000, order = 512), punif) ks_19937 <- ks.test(WELL(10000, order = 19937, temper = TRUE), punif) print(c(ks_512$statistic, ks_19937$statistic)) ``` -------------------------------- ### Retrieve Current Generator State Source: https://context7.com/cran/randtoolbox/llms.txt The get.description function retrieves metadata and internal state information about the currently active random number generator configured via set.generator. ```r library(randtoolbox) library(rngWELL) set.generator("WELL", order = 19937, version = "a", seed = 42) desc <- get.description() print(desc$name) print(desc$parameters) print(desc$authors) print(length(desc$state)) ``` -------------------------------- ### Configure Random Number Generator for runif Source: https://context7.com/cran/randtoolbox/llms.txt The set.generator function allows users to replace R's default random number generator with alternatives like WELL, Mersenne Twister, or congruRand. Once configured, standard R functions like runif, rnorm, and rexp will utilize the selected generator. ```r library(randtoolbox) library(rngWELL) set.generator("WELL", order = 19937, version = "a", seed = 12345) x <- runif(10) set.generator("MersenneTwister", initialization = "init2002", resolution = "32", seed = 12345) x_mt <- runif(10) set.generator("congruRand", mod = 2^31 - 1, mult = 16807, incr = 0, seed = 12345) x_lcg <- runif(10) set.generator("default", seed = 42) x_default <- runif(10) ``` -------------------------------- ### Knuth TAOCP 2002 Pseudo-Random Number Generator (R) Source: https://context7.com/cran/randtoolbox/llms.txt Generates pseudo-random numbers using Knuth's lagged Fibonacci generator (x_n = (x_{n-37} + x_{n-100}) mod 2^30). This is a fast generator suitable for simulations and Monte Carlo methods, supporting multi-dimensional output and ensuring reproducibility. ```R library(randtoolbox) # Basic usage setSeed(42) x <- knuthTAOCP(1000) print(summary(x)) # Single dimension output setSeed(123) x <- knuthTAOCP(10) print(x) # Multi-dimensional output setSeed(456) matrix_out <- knuthTAOCP(n = 500, dim = 5) print(dim(matrix_out)) # Reproducibility setSeed(1302) x1 <- knuthTAOCP(5) setSeed(1302) x2 <- knuthTAOCP(5) print(identical(x1, x2)) # Performance comparison x <- knuthTAOCP(1000000) print(length(x)) ``` -------------------------------- ### Halton Quasi-Random Number Generator (R) Source: https://context7.com/cran/randtoolbox/llms.txt Generates Halton low-discrepancy sequences using Van der Corput sequences in different prime bases. This is a fundamental quasi-random sequence generator useful for numerical integration and Monte Carlo methods, supporting multi-dimensional output. ```R library(randtoolbox) # Basic Halton sequence x <- halton(n = 10, dim = 1) print(x) # Multi-dimensional Halton sequence x <- halton(n = 10, dim = 5) print(x[1:5, ]) ``` -------------------------------- ### Compute Stirling Numbers of the Second Kind in R Source: https://context7.com/cran/randtoolbox/llms.txt Calculates Stirling numbers of the second kind, which represent the number of ways to partition a set of n elements into k non-empty subsets. This function is useful for combinatorial analysis and internal collision tests. ```R library(randtoolbox) s4 <- stirling(4) print(s4) bell_5 <- sum(stirling(5)) print(bell_5) ``` -------------------------------- ### Perform Order Test for Randomness Source: https://context7.com/cran/randtoolbox/llms.txt The order.test function evaluates the randomness of a sequence by analyzing the distribution of permutations in consecutive d-tuples. It returns a list containing the chi-squared statistic, p-value, and observed counts, with a practical limit of d=8 due to computational complexity. ```r library(randtoolbox) setSeed(42) x <- SFMT(12000) result <- order.test(x, d = 3, echo = TRUE) print(result$statistic) print(result$p.value) result_2 <- order.test(SFMT(10000), d = 2, echo = FALSE) result_4 <- order.test(SFMT(12000), d = 4, echo = FALSE) result_5 <- order.test(SFMT(12000), d = 5, echo = FALSE) print(c(result_2$p.value, result_4$p.value, result_5$p.value)) ``` -------------------------------- ### Perform Poker Test on Random Sequences Source: https://context7.com/cran/randtoolbox/llms.txt The poker test evaluates randomness by analyzing the distribution of patterns (hands) in groups of consecutive numbers. It checks if the observed frequency of patterns matches the expected theoretical distribution. ```R library(randtoolbox) # Basic poker test setSeed(42) x <- SFMT(10000) result <- poker.test(x, nbcard = 5, echo = TRUE) # Test all pseudo-random generators setSeed(123) for (gen_name in c("SFMT", "WELL", "congruRand", "knuthTAOCP")) { x <- switch(gen_name, SFMT = SFMT(10000), WELL = WELL(10000), congruRand = congruRand(10000), knuthTAOCP = knuthTAOCP(10000)) result <- poker.test(x, nbcard = 5, echo = FALSE) cat(sprintf("%s: p-value = %.4f\n", gen_name, result$p.value)) } ``` -------------------------------- ### poker.test - Poker Test Source: https://context7.com/cran/randtoolbox/llms.txt Tests randomness using the poker test, examining the distribution of 'hands' (patterns of repeated values) in groups of consecutive numbers. ```APIDOC ## poker.test - Poker Test ### Description Tests randomness using the poker test, examining the distribution of "hands" (patterns of repeated values) in groups of consecutive numbers. ### Method ```r poker.test(x, nbcard = 5, echo = TRUE) ``` ### Parameters #### Arguments - **x** (numeric vector) - The sequence of random numbers to test. Its length must be a multiple of nbcard. - **nbcard** (integer) - The number of cards (values) in each hand. Defaults to 5. - **echo** (logical) - If TRUE, prints the test results. Defaults to TRUE. ### Request Example ```r library(randtoolbox) setSeed(42) x <- SFMT(10000) result <- poker.test(x, nbcard = 5, echo = TRUE) ``` ### Response #### Success Response (200) Returns a list containing the chi-square statistic, degrees of freedom, p-value, and observed frequencies for each hand type. - **statistic** (numeric) - The calculated chi-square statistic. - **p.value** (numeric) - The p-value of the test. - **observed** (numeric vector) - The observed counts for each poker hand type. #### Response Example ```json { "statistic": 3.456, "p.value": 0.4843, "observed": [24, 482, 1234, 218, 42] } ``` ``` -------------------------------- ### Perform Frequency Test on Random Sequences Source: https://context7.com/cran/randtoolbox/llms.txt The frequency test uses a chi-square goodness-of-fit test to determine if values are uniformly distributed across defined bins. It is a fundamental test for checking the distribution properties of a generator. ```R library(randtoolbox) # Basic frequency test setSeed(42) x <- WELL(10000, order = 19937) result <- freq.test(x, seq = 0:15, echo = TRUE) # Compare multiple generators generators <- list(SFMT = SFMT(10000), WELL = WELL(10000, order = 512), congruRand = congruRand(10000), knuthTAOCP = knuthTAOCP(10000)) for (name in names(generators)) { result <- freq.test(generators[[name]], seq = 0:15, echo = FALSE) cat(sprintf("%s: chi-sq = %.2f, p-value = %.4f\n", name, result$statistic, result$p.value)) } ``` -------------------------------- ### Perform Collision Test for Randomness Source: https://context7.com/cran/randtoolbox/llms.txt The coll.test function tests randomness by counting collisions when mapping values to cells, based on the birthday paradox. It includes a sparse variant for large cell numbers and supports various generator functions as input. ```r library(randtoolbox) result <- coll.test(SFMT, lenSample = 2^8, segments = 2^5, tdim = 2, nbSample = 500, echo = TRUE) result_well <- coll.test(WELL, lenSample = 2^8, segments = 2^5, tdim = 2, nbSample = 500, echo = FALSE, order = 512) result_sparse <- coll.test.sparse(SFMT, lenSample = 2^14, segments = 2^10, tdim = 2, nbSample = 10) print(result_sparse) ``` -------------------------------- ### Perform Gap Test for Randomness Source: https://context7.com/cran/randtoolbox/llms.txt Conducts a gap test to assess the randomness of a sequence by examining the distribution of gaps between values falling within a specified interval. It uses a chi-square goodness-of-fit test. ```r library(randtoolbox) # Basic gap test on pseudo-random sequence setSeed(42) x <- SFMT(10000) result <- gap.test(x, lower = 0, upper = 0.5, echo = TRUE) ``` -------------------------------- ### setSeed - Set the Random Seed Source: https://context7.com/cran/randtoolbox/llms.txt Sets the seed for all randtoolbox generators. This function is crucial for ensuring reproducibility of random number sequences generated by the package's functions. ```APIDOC ## setSeed - Set the Random Seed ### Description Sets the seed for all randtoolbox generators (congruRand, SFMT, WELL, knuthTAOCP). Unlike R's set.seed(), this function affects only randtoolbox generators and should use seeds with good binary representation (avoid seeds with too few ones). ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters * **seed** (numeric) - Required - The seed value to initialize the random number generator. ### Request Example ```R library(randtoolbox) # Set seed using a memorable date (e.g., birthday: day/month/year) setSeed(15061985) # Generate reproducible sequence x1 <- SFMT(5, usepset = FALSE) print(x1) ``` ### Response N/A (modifies internal state) ### Response Example ```R # [1] 0.7823456 0.3421987 0.9876543 0.1234567 0.5678901 ``` ``` -------------------------------- ### freq.test - Frequency Test Source: https://context7.com/cran/randtoolbox/llms.txt Tests whether values are uniformly distributed across equal-width bins using the chi-square goodness-of-fit test. ```APIDOC ## freq.test - Frequency Test ### Description Tests whether values are uniformly distributed across equal-width bins using chi-square goodness-of-fit. ### Method ```r freq.test(x, seq = 0:15, echo = TRUE) ``` ### Parameters #### Arguments - **x** (numeric vector) - The sequence of random numbers to test. - **seq** (numeric vector) - The sequence defining the bin edges. Defaults to 0:15. - **echo** (logical) - If TRUE, prints the test results. Defaults to TRUE. ### Request Example ```r library(randtoolbox) setSeed(42) x <- WELL(10000, order = 19937) result <- freq.test(x, seq = 0:15, echo = TRUE) ``` ### Response #### Success Response (200) Returns a list containing the chi-square statistic, degrees of freedom, p-value, sample size, observed frequencies, and expected frequencies. - **statistic** (numeric) - The calculated chi-square statistic. - **p.value** (numeric) - The p-value of the test. - **observed** (numeric vector) - The observed frequencies in each bin. #### Response Example ```json { "statistic": 12.456, "p.value": 0.6432, "observed": [623, 641, 612, 628, 635, 619, 631, 627, 618, 642, 615, 639, 620, 634, 626, 640] } ``` ``` -------------------------------- ### Perform Gap Test on Random Sequences Source: https://context7.com/cran/randtoolbox/llms.txt The gap test evaluates the distance between occurrences of numbers within a specified interval. It helps detect non-random clustering in the sequence. ```R library(randtoolbox) # Test with different interval result2 <- gap.test(x, lower = 0.25, upper = 0.75, echo = FALSE) print(result2$p.value) # Compare generators setSeed(1) x_congru <- congruRand(10000) result_congru <- gap.test(x_congru, lower = 0, upper = 0.5, echo = FALSE) print(paste("congruRand p-value:", round(result_congru$p.value, 4))) # Test quasi-random sequence x_sobol <- sobol(10000) result_sobol <- gap.test(x_sobol, lower = 0, upper = 0.5, echo = FALSE) print(paste("Sobol p-value:", round(result_sobol$p.value, 4))) ``` -------------------------------- ### Prime Number Retrieval Source: https://context7.com/cran/randtoolbox/llms.txt Retrieves prime numbers from an internal table. This function is primarily used by quasi-random generators but can be utilized for other applications requiring prime numbers. ```APIDOC ## get.primes - Retrieve Prime Numbers ### Description Returns the first n prime numbers from an internal table of 100,000 primes. Used internally by quasi-random generators but available for user applications. ### Method `get.primes(n)` ### Parameters #### Path Parameters - **n** (integer) - Required - The number of prime numbers to retrieve. ### Request Example ```r library(randtoolbox) # Get the first 20 prime numbers primes <- get.primes(20) print(primes) ``` ### Response #### Success Response (200) - **primes** (vector) - A vector containing the first n prime numbers. #### Response Example ```r # Example output for get.primes(10) # [1] 2 3 5 7 11 13 17 19 23 29 ``` ``` -------------------------------- ### Gap Test for Randomness Source: https://context7.com/cran/randtoolbox/llms.txt Performs a gap test to assess the randomness of a sequence by examining the distribution of gaps between values falling within a specified interval. ```APIDOC ## gap.test - Gap Test ### Description Tests randomness by examining the distribution of gaps between values falling within a specified interval [lower, upper]. Based on chi-square goodness-of-fit. ### Method `gap.test(x, lower, upper, echo = TRUE)` ### Parameters #### Path Parameters - **x** (vector) - Required - The sequence of numbers to test. - **lower** (numeric) - Required - The lower bound of the interval. - **upper** (numeric) - Required - The upper bound of the interval. - **echo** (boolean) - Optional - If TRUE, prints the test results. Defaults to TRUE. ### Request Example ```r library(randtoolbox) # Perform a gap test on a sequence setSeed(42) x <- SFMT(10000) # Assuming SFMT is available and generates a sequence result <- gap.test(x, lower = 0, upper = 0.5) print(result) ``` ### Response #### Success Response (200) - **result** (list) - A list containing the results of the gap test, including chi-square statistic, degrees of freedom, and p-value. #### Response Example ```r # Example output for gap.test(x, lower = 0, upper = 0.5) # chisq stat = 8.234, df = 12, p-value = 0.7654 # (sample size: 10000) # length observed freq theoretical freq ``` ``` -------------------------------- ### Perform Serial Test on Random Sequences Source: https://context7.com/cran/randtoolbox/llms.txt The serial test examines the independence of consecutive pairs of values by mapping them into a d x d grid. It is effective for identifying patterns or dependencies between sequential numbers. ```R library(randtoolbox) # Basic serial test setSeed(42) x <- SFMT(10000) result <- serial.test(x, d = 8, echo = TRUE) # Compare to known bad generator setSeed(1) x_lcg <- congruRand(10000, mod = 2^16, mult = 75, incr = 0) result_lcg <- serial.test(x_lcg, d = 8, echo = FALSE) print(result_lcg$p.value) ``` -------------------------------- ### serial.test - Serial Test Source: https://context7.com/cran/randtoolbox/llms.txt Tests independence between consecutive pairs of values by examining the joint distribution in a d x d grid. ```APIDOC ## serial.test - Serial Test ### Description Tests independence between consecutive pairs of values by examining the joint distribution in a d x d grid. ### Method ```r serial.test(x, d = 8, echo = TRUE) ``` ### Parameters #### Arguments - **x** (numeric vector) - The sequence of random numbers to test. Its length must be a multiple of d*2. - **d** (integer) - The dimension of the grid. Defaults to 8. - **echo** (logical) - If TRUE, prints the test results. Defaults to TRUE. ### Request Example ```r library(randtoolbox) setSeed(42) x <- SFMT(10000) result <- serial.test(x, d = 8, echo = TRUE) ``` ### Response #### Success Response (200) Returns a list containing the chi-square statistic, degrees of freedom, and p-value. - **statistic** (numeric) - The calculated chi-square statistic. - **p.value** (numeric) - The p-value of the test. - **parameter** (integer) - The degrees of freedom for the test. #### Response Example ```json { "statistic": 58.234, "p.value": 0.6543, "parameter": 63 } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.