Language | Basic function | Kind | Range | Open/Close |
---|---|---|---|---|
Fortran | random_number() |
real32/64 | [0,1) | N/A |
C | rand() |
integer | [0,RAND_MAX] | N/A |
C++ | uniform_real_distribution() |
real32/64 | [a,b) | N/A |
Rust | .gen() |
real64 | [0,1) | N/A |
Go | .Float64() and .Float32() |
real64 | [0,1) | N/A |
.Net framework | NextDouble() |
real64 | [0,1) | N/A |
gawk | rand() |
real | [0,1) | N/A |
Perl | rand() |
real | [0,1) | N/A |
Ruby | .rand() |
real | [0,1) | N/A |
Python | random() |
real | [0,1) | N/A |
NumPy | random() |
real64 | [0,1) | N/A |
Julia | rand() |
real64 | [0,1) | N/A |
MATLAB | rand() |
real64 | (0,1) | N/A |
GNU Octave | rand() |
real64 | (0,1) | N/A |
Scilab | rand() |
real64 | [0,1) | N/A |
R | runif() |
real64 | (0,1) comment | N/A |
SAS | RAND('UNIFORM') |
real | (0,1) | N/A |
Mathematica | RandomReal[] |
real | [0,1] comment | N/A |
Maxima | random() |
real | [0,1) | N/A |
Maple | rand() |
real | [0,1] | N/A |
Microsoft Excel | RAND() |
real | [0,1) | N/A |
MKL | vdrnguniform() |
real32/64 | [0,1) | N/A |
NAG | G05SAF() |
real32/64 | (0,1] | [a,b] by another subroutine |
IMSL | RNUN() |
real32/64 | (0,1) | [0,1) by Mersenne Twister |
HSL | FA04AD() |
real64 | (0,1) | N/A |
RANLIB | ranf() |
real32 | (0,1) | N/A |
GSL | gsl_rng_uniform() |
real64 | [0,1) | (0,1) by another subroutine |
Mersenne Twister | genrand_res53() |
real64 | [0,1) | [0,1] and (0,1) |
cuRAND | curand_uniform() |
real32/64 | (0,1] | N/A |
AMD RNG | DRANDUNIFORM() |
real64 | (0,1] old doc | N/A |
// random bit engine with seed
std::default_random_engine rng(123456789);
std::cout << "Random bits: " << rng() << '\n';
// specific engine ex. Mersenne Twister
// std::mt19937 rng(123456789);
// use an array for seed
// std::seed_seq seedarray{1,2,3,4,5};
// std::mt19937 rng(seedarray);
// re-seed
rng.seed(1);
// distribution with fixed parameters: [0,1)
std::uniform_real_distribution<double> unif_rand(0, 1);
// call it with the engine object
std::cout << "Random numbers [0,1): " << unif_rand(rng) << '\n';
// changing the parameters [7,10)
unif_rand.param( decltype(unif_rand)::param_type{ 7, 10 } ) ;
std::cout << "Random numbers [7,10): " << unif_rand(rng) << '\n';
Similar to C++ (no code here).
using Random
using Distributions
# make a generator
rng = MersenneTwister(123456789);
# random bits
rand(rng,UInt64)
# re-seed
Random.seed!(rng,543210)
# uniform real distribution [0,1)
rand(rng)
# uniform real distribution [7,10)
rand(rng,Uniform(0,10))
from numpy.random import Generator, PCG64, MT19937, SeedSequence
# make a generator
seed = SeedSequence(1234)
rg = Generator(MT19937(seed))
# simple version
# rg = Generator(MT19937(1234))
# random bits
rg.bytes(8)
# re-seed
rg = Generator(MT19937(seed))
# uniform real [0,1)
rg.random()
# uniform real [7,10)
rg.uniform(7,10)
type(VSL_STREAM_STATE) :: stream
integer :: status,brng,seed,method
integer(int64) :: b
real(real64) :: d
! brng = engine
brng = VSL_BRNG_MT19937
seed = 123456789
status = vslnewstream( stream, brng, seed )
! random bits
method = VSL_RNG_METHOD_UNIFORMBITS64_STD
status = virnguniformbits64( method, stream, 1, b )
! re-seed
status = vslnewstream( stream, brng, seed )
! uniform real [0,1)
method = VSL_RNG_METHOD_UNIFORM_STD
status = vdrnguniform( method, stream, 1, d, 0d0, 1d0 )
! uniform real [7,10)
method = VSL_RNG_METHOD_UNIFORM_STD
status = vdrnguniform( method, stream, 1, d, 7d0, 10d0 )
integer,parameter :: LSEED=1, LSTATE=17
integer :: GENID, SUBID, SEED(LSEED),STATE(LSTATE), IFAIL
real(real64) :: d
! GENID = engine
GENID = 1 ! default
SEED = 123456789
call G05KFF(GENID, SUBID, SEED, LSEED, STATE, LSTATE, IFAIL)
! no random bits
! uniform real [0,1)
G05SAF(1, STATE, d, IFAIL)
! uniform real [7,10)
call G05SQF(1, 7d0, 10d0, STATE, d, IFAIL)