Random number generator (included): Difference between revisions

m
imported>Arakov
 
(119 intermediate revisions by 65 users not shown)
Line 1:
{{Task}}
The task is to:
: State the type of random number generator algorithm used in a languageslanguage's built-in random number generator, or. omitIf the language ifor noits randomimmediate numberlibraries generatordon't isprovide givena asrandom partnumber ofgenerator, theskip languagethis or its immediate librariestask.<br>
: If possible, give a link to a wider [[wp:List of random number generators|explanation]] of the algorithm used should be given.
 
<small>Note: the task is ''not'' to create an RNG, but to report on the languages in-built RNG that would be the most likely RNG used.</small>
 
The main types of pseudo-random number generator, ([[wp:PRNG|PRNG]]), that are in use are the [[linear congruential generator|Linear Congruential Generator]], ([[wp:Linear congruential generator|LCG]]), and the Generalized Feedback Shift Register, ([[wp:Generalised_feedback_shift_register#Non-binary_Galois_LFSR|GFSR]]), (of which the [[wp:Mersenne twister|Mersenne twister]] generator is a subclass). The last main type is where the output of one of the previous ones (typically a Mersenne twister) is fed through a [[cryptographic hash function]] to maximize unpredictability of individual bits.
 
Note that neither LCGs nor GFSRs should be used for the most demanding applications (cryptography) without additional steps.
 
=={{header|11l}}==
11l uses a [[wp:Linear congruential generator|linear congruential generator]] accessed via the built-in [http://11l-lang.org/doc/built-in-modules/random random module].
 
=={{header|8th}}==
The default random number generator in 8th is a cryptographically strong one using [https://en.wikipedia.org/wiki/Fortuna_%28PRNG%29 Fortuna], which is seeded from the system's entropy provider. An additional random generator (which is considerably faster) is a [http://www.pcg-random.org/ PCG], though it is not cryptographically strong.
 
=={{header|ActionScript}}==
Line 20 ⟶ 26:
The used algorithm is implementation defined. The standard says: "To enable the user to determine the suitability of the random number generators for the intended application, the implementation shall describe the algorithm used and shall give its period, if known exactly, or a lower bound on the period, if the exact period is unknown."
 
* [http://www.adahome.com/rm95/rm9x-A-05-02.html| Ada 95 RM - A.5.2 Random Number Generation]
* [http://www.adaic.com/standards/05rm/html/RM-A-5-2.html| Ada 2005 RM - A.5.2 Random Number Generation]
* [http://www.adaic.org/resources/add_content/standards/12rm/html/RM-A-5-2.html Ada 2012 RM - A.5.2 Random Number Generation]
 
=={{header|ALGOL 68}}==
Line 28 ⟶ 35:
* [http://vestein.arb-phys.uni-dortmund.de/~wb/RR/rrA5.html 10.5. The particular preludes and postlude - 10.5.1. The particular preludes]
<langsyntaxhighlight lang="algol68">PROC ℒ next random = (REF ℒ INT a)ℒ REAL: ( a :=
¢ the next pseudo-random ℒ integral value after 'a' from a
uniformly distributed sequence on the interval [ℒ 0,ℒ maxint] ¢;
Line 39 ⟶ 46:
 
INT ℒ last random := # some initial random number #;
PROC ℒ random = ℒ REAL: ℒ next random(ℒ last random);</langsyntaxhighlight>
 
Note the suitable "next random number" is suggested to be: ( a := &cent; the next pseudo-random ℒ integral value after 'a' from a uniformly distributed sequence on the interval [ℒ 0,ℒ maxint] &cent;; &cent; the real value corresponding to 'a' according to some mapping of integral values [ℒ 0, ℒ max int] into real values [ℒ 0, ℒ 1) i.e., such that -0 <= x < 1 such that the sequence of real values so produced preserves the properties of pseudo-randomness and uniform distribution of the sequence of integral values &cent;);
Line 46 ⟶ 53:
 
For an ASCII implementation and for '''long real''' precision these routines would appears as:
<langsyntaxhighlight lang="algol68">PROC long next random = (REF LONG INT a)LONG REAL: # some suitable next random number #;
INT long last random := # some initial random number #;
PROC long random = LONG REAL: long next random(long last random);</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
The built-in [https://arturo-lang.io/documentation/library/numbers/random/ random] is based on Nim's random number generator and, thus, in turn based on xoroshiro128+ (xor/rotate/shift/rotate), see [http://xoroshiro.di.unimi.it/ here].
 
=={{header|AutoHotkey}}==
The built-in command [http://www.autohotkey.com/docs/commands/Random.htm Random] generates a pseudo-random number using Mersenne Twister "MT19937" (see documentation).
 
=={{header|AWK}}==
The built-in command "rand" generates a pseudo-random uniform distributed random variable. More information is available from the documentation of
[http://www.gnu.org/software/gawk/manual/html_node/Numeric-Functions.html gawk].
 
It is important that the RNG is seeded with the funtions "srand", otherwise,
the same random number is produced.
 
Example usage: see [http://rosettacode.org/wiki/Random_number_generator_(included)#UNIX_Shell #UNIX_Shell]
 
=={{header|BASIC}}==
 
The RND function generates a pseudo random number greater than or equal to zero, but less than one. The implementation is machine specific based on contents of the ROM and there is no fixed algorithm.
 
=={{header|Batch File}}==
Windows batch files can use the <code>%RANDOM%</code> pseudo-variable which returns a pseudo-random number between 0 and 32767. Behind the scenes this is just a call to the C runtime's <code>rand()</code> function which uses an LCG in this case:
:<math>X_{n+1}=X_n\cdot 214013 + 2531011 \pmod {2^{15}}</math>
 
=={{header|BBC BASIC}}==
The RND function uses a 33-bit maximal-length Linear Feedback Shift Register (LFSR), with 32-bits being used to provide the result. Hence the sequence length is 2^33-1, during which the value zero is returned once and all non-zero 32-bit values are each returned twice.
 
=={{header|Befunge}}==
The ? instruction usually uses the random number generator in the interpreter's language. The original interpreter is written in C and uses rand().
 
=={{header|C}}==
Line 83 ⟶ 113:
BSD rand() produces a cycling sequence of only <math>2^{31}</math> possible states; this is already too short to produce good random numbers. The big problem with BSD rand() is that the low <math>n</math> bits' cycle sequence length is only <math>2^n</math>. (This problem happens because the modulus <math>2^{31}</math> is a power of two.) The worst case, when <math>n = 1</math>, becomes obvious if one uses the low bit to flip a coin.
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 95 ⟶ 125:
puts((rand() % 2) ? "heads" : "tails");
return 0;
}</langsyntaxhighlight>
 
If the C compiler uses BSD rand(), then this program has only two possible outputs.
Line 126 ⟶ 156:
=={{header|C sharp}}==
The .NET Random class says that it uses Knuth's subtractive random number generator algorithm.[http://msdn.microsoft.com/en-us/library/system.random.aspx#remarksToggle]
 
=={{header|C++}}==
As part of the C++11 specification the language now includes various forms of random number generation.
 
While the default engine is implementation specific (ex, unspecified), the following Pseudo-random generators are available in the standard:
* Linear congruential (minstd_rand0, minstd_rand)
* Mersenne twister (mt19937, mt19937_64)
* Subtract with carry (ranlux24_base, ranlux48_base)
* Discard block (ranlux24, ranlux48)
* Shuffle order (knuth_b)
 
Additionally, the following distributions are supported:
* Uniform distributions: uniform_int_distribution, uniform_real_distribution
* Bernoulli distributions: bernoulli_distribution, geometric_distribution, binomial_distribution, negative_binomial_distribution
* Poisson distributions: poisson_distribution, gamma_distribution, exponential_distribution, weibull_distribution, extreme_value_distribution
* Normal distributions: normal_distribution, fisher_f_distribution, cauchy_distribution, lognormal_distribution, chi_squared_distribution, student_t_distribution
* Sampling distributions: discrete_distribution, piecewise_linear_distribution, piecewise_constant_distribution
 
Example of use:
{{works with|C++11}}
<syntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <random>
int main()
{
std::random_device rd;
std::uniform_int_distribution<int> dist(1, 10);
std::mt19937 mt(rd());
std::cout << "Random Number (hardware): " << dist(rd) << std::endl;
std::cout << "Mersenne twister (hardware seeded): " << dist(mt) << std::endl;
}</syntaxhighlight>
 
=={{header|Chapel}}==
When using the [https://chapel-lang.org/docs/modules/standard/Random.html <code>Random</code> module], Chapel defaults to a [http://www.pcg-random.org/ Permuted Linear Congruential Random Number Generator].
 
=={{header|Clojure}}==
Line 133 ⟶ 199:
CMake has a random ''string'' generator.
 
<langsyntaxhighlight lang="cmake"># Show random integer from 0 to 9999.
string(RANDOM LENGTH 4 ALPHABET 0123456789 number)
math(EXPR number "${number} + 0") # Remove extra leading 0s.
message(STATUS ${number})</langsyntaxhighlight>
 
The current implementation (in [http://cmake.org/gitweb?p=cmake.git;a=blob;f=Source/cmStringCommand.cxx;hb=HEAD cmStringCommand.cxx] and [http://cmake.org/gitweb?p=cmake.git;a=blob;f=Source/cmSystemTools.cxx;hb=HEAD cmSystemTools.cxx]) calls [[{{PAGENAME}}#C|rand() and srand() from C]]. It picks random letters from the alphabet. The probability of each letter is near ''1 &divide; length'', but the implementation uses floating-point arithmetic to map ''RAND_MAX + 1'' values onto ''length'' letters, so there is a small modulo bias when ''RAND_MAX + 1'' is not a multiple of ''length''.
Line 143 ⟶ 209:
 
CMake 2.8.5 tries a [[random number generator (device)|secure seed]] (CryptGenRandom or /dev/urandom) or falls back to high-resolution [[system time]]. Older versions seed the random generator with <code>time(NULL)</code>, the current time in seconds.
 
=={{header|Common Lisp}}==
The easiest way to generate random numbers in Common Lisp is to use the built-in rand function after seeding the random number generator. For example, the first line seeds the random number generator and the second line generates a number from 0 to 9
<syntaxhighlight lang="lisp">(setf *random-state* (make-random-state t))
(random 10)</syntaxhighlight>
[https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node133.html Common Lisp: The Language, 2nd Ed.] does not specify a specific random number generator algorithm, nor a way to use a user-specified seed.
 
=={{header|D}}==
Line 151 ⟶ 223:
=={{header|Delphi}}==
According to [[wp:Linear_congruential_generator#Parameters_in_common_use|Wikipedia]], Delphi uses a Linear Congruential Generator.
 
Random functions:
function Random : Extended;
function Random ( LimitPlusOne : Integer ) : Integer;
procedure Randomize;
 
Based on the values given in the wikipedia entry here is a Delphi compatible implementation for use in other pascal dialects.
<syntaxhighlight lang="pascal">
unit delphicompatiblerandom;
{$ifdef fpc}{$mode objfpc}{$endif}
interface
function LCGRandom: extended; overload;inline;
function LCGRandom(const range:longint):longint;overload;inline;
implementation
function IM:cardinal;inline;
begin
RandSeed := RandSeed * 134775813 + 1;
Result := RandSeed;
end;
function LCGRandom: extended; overload;inline;
begin
Result := IM * 2.32830643653870e-10;
end;
function LCGRandom(const range:longint):longint;overload;inline;
begin
Result := IM * range shr 32;
end;
end.</syntaxhighlight>
 
=={{header|DWScript}}==
DWScript currently uses a 64bit [[wp:Xorshift|XorShift]] PRNG, which is a fast and light form of GFSR.
 
=={{header|Déjà Vu}}==
The standard implementation, <code>[[vu]]</code>, uses a Mersenne twister.
 
<syntaxhighlight lang="dejavu">!print random-int # prints a 32-bit random integer</syntaxhighlight>
 
=={{header|EchoLisp}}==
EchoLisp uses an ARC4 (or RCA4) implementation by David Bau, which replaces the JavaScript Math.random(). Thanks to him. [https://github.com/davidbau/seedrandom].
Some examples :
<syntaxhighlight lang="lisp">
(random-seed "albert")
(random) → 0.9672510261922906 ; random float in [0 ... 1[
(random 1000) → 726 ; random integer in [0 ... 1000 [
(random -1000) → -936 ; random integer in ]-1000 1000[
 
(lib 'bigint)
(random 1e200) → 48635656441292641677...3917639734865662239925...9490799697903133046309616766848265781368
</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 6.x :
<syntaxhighlight lang="elena">import extensions;
public program()
{
console.printLine(randomGenerator.nextReal());
console.printLine(randomGenerator.nextInt(0,100))
}</syntaxhighlight>
{{out}}
<pre>
0.706398
46
</pre>
 
=={{header|Elixir}}==
Elixir does not come with its own module for random number generation. But you can use the appropriate Erlang functions instead. Some examples:
<syntaxhighlight lang="elixir">
# Seed the RNG
:random.seed(:erlang.now())
 
# Integer in the range 1..10
:random.uniform(10)
 
# Float between 0.0 and 1.0
:random.uniform()
</syntaxhighlight>
For further information, read the Erlang section.
 
=={{header|Erlang}}==
Line 165 ⟶ 319:
 
Seed with a fixed known value triplet A1, A2, A3:
<syntaxhighlight lang="erlang">
<lang Erlang>
random:seed(A1, A2, A3)
</syntaxhighlight>
</lang>
Example with the running time:
<syntaxhighlight lang="erlang">
<lang Erlang>
...
{A1,A2,A3} = erlang:now(),
random:seed(A1, A2, A3),
...sequence of randoms used
random:seed(A1, A2, A3),
...same sequence of randoms used
</syntaxhighlight>
</lang>
Get a random float value between 0.0 and 1.0:
<syntaxhighlight lang="erlang">
<lang Erlang>
Rfloat = random:uniform(),
</syntaxhighlight>
</lang>
Get a random integer value between 1 and N (N is an integer >= 1):
<syntaxhighlight lang="erlang">
<lang Erlang>
Rint = random:uniform(N),
</syntaxhighlight>
</lang>
 
=={{header|Euler Math Toolbox}}==
 
Bays and Durham as describend in Knuth's book.
 
=={{header|Factor}}==
The default RNG used when the <code>random</code> vocabulary is used, is the [[wp:Mersenne twister|Mersenne twister]] algorithm [http://docs.factorcode.org/content/article-random.html]. But there are other RNGs available, including [[wp:SFMT|SFMT]], the system RNG ([[wp:/dev/random|/dev/random]] on Unix) and [[wp:Blum Blum Shub|Blum Blum Shub]]. It's also very easy to implement your own RNG and integrate it into the system. [http://docs.factorcode.org/content/article-random-protocol.html]
 
=={{header|Fortran}}==
Fortran has intrinsic random_seed() and random_number() subroutines. Used algorithm of the pseudorandom number generator is compiler dependent (not specified in ISO Fortran Standard, see ISO/IEC 1539-1:2010 (E), 13.7.135 RANDOM NUMBER). For algorithm in GNU gfortran see https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fNUMBER.html
Note that with the GNU gfortran compiler program needs to call random_seed with a random PUT= argument to get a pseudorandom number otherwise the sequence always starts with the same number. Intel compiler ifort reinitializes the seed randomly without PUT argument to random value using the system date and time. Here we are seeding random_seed() with some number obtained from the Linux urandom device.
 
<syntaxhighlight lang="fortran">
program rosetta_random
implicit none
 
integer, parameter :: rdp = kind(1.d0)
real(rdp) :: num
integer, allocatable :: seed(:)
integer :: un,n, istat
 
call random_seed(size = n)
allocate(seed(n))
 
! Seed with the OS random number generator
open(newunit=un, file="/dev/urandom", access="stream", &
form="unformatted", action="read", status="old", iostat=istat)
if (istat == 0) then
read(un) seed
close(un)
end if
call random_seed (put=seed)
call random_number(num)
write(*,'(E24.16)') num
end program rosetta_random
</syntaxhighlight>
 
=={{header|Free Pascal}}==
 
<syntaxhighlight lang="pascal">
program RandomNumbers;
// Program to demonstrate the Random and Randomize functions.
var
RandomInteger: integer;
RandomFloat: double;
begin
Randomize; // generate a new sequence every time the program is run
RandomFloat := Random(); // 0 <= RandomFloat < 1
Writeln('Random float between 0 and 1: ', RandomFloat: 5: 3);
RandomFloat := Random() * 10; // 0 <= RandomFloat < 10
Writeln('Random float between 0 and 10: ', RandomFloat: 5: 3);
RandomInteger := Random(10); // 0 <= RandomInteger < 10
Writeln('Random integer between 0 and 9: ', RandomInteger);
// Wait for <enter>
Readln;
end.
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
FreeBASIC has a Rnd() function which produces a pseudo-random double precision floating point number in the half-closed interval [0, 1) which can then be easily used to generate pseudo-random numbers (integral or decimal) within any range.
 
The sequence of pseudo-random numbers can either by seeded by a parameter to the Rnd function itself or to the Randomize statement and, if omitted, uses a seed based on the system timer.
 
However, a second parameter to the Randomize statement determines which of 5 different algorithms is used to generate the pseudo-random numbers:
 
1. Uses the C runtime library's rand() function (based on LCG) which differs depending on the platform but produces a low degree of randomness.
 
2. Uses a fast, platform independent, algorithm with 32 bit granularity and a reasonable degree of randomness. The basis of this algorithm is not specified in the language documentation.
 
3. Uses the Mersenne Twister algorithm (based on GFSR) which is platform independent, with 32 bit granularity and a high degree of randomness. This is good enough for most non-cryptographic purposes.
 
4. Uses a QBASIC compatible algorithm which is platform independent, with 24 bit granularity and a low degree of randomness.
 
5. Uses system features (Win32 Crypto API or /dev/urandom device on Linux) to generate pseudo-random numbers, with 32 bit granularity and a very high degree of randomness (cryptographic strength).
 
A parameter of 0 can also be used (and is the default if omitted) which uses algorithm 3 in the -lang fb dialect, 4 in the -lang qb dialect and 1 in the -lang fblite dialect.
 
=={{header|FutureBasic}}==
Syntax:
<pre>
randomInteger = rnd(expr)
</pre>
This function returns a pseudo-random long integer uniformly distributed in the range 1 through expr. The expr parameter should be greater than 1, and must not exceed 65536. If the value returned is to be assigned to a 16-bit integer (randomInteger), expr should not exceed 32767. The actual sequence of numbers returned by rnd depends on the random number generator's "seed" value. (Note that rnd(1) always returns the value 1.)
 
Syntax:
<pre>
random (or randomize) [expr]
</pre>
This statement "seeds" the random number generator: this affects the sequence of values which are subsequently returned by the rnd function and the maybe function. The numbers returned by rnd and maybe are not truly random, but follow a "pseudo-random" sequence which is uniquely determined by the seed number (expr). If you use the same seed number on two different occasions, you'll get the same sequence of "random" numbers both times. When you execute random without any expr parameter, the system's current time is used to seed the random number generator.
 
Example 1:
<pre>
random 375 // using seed number
</pre>
Example 2:
<pre>
random // current system time used as seed
</pre>
 
Example:
To get a random integer between two arbitrary limits min and max, use the following statement. (Note: max - min must be less than or equal to 65536.):
<pre>
randomInteger = rnd(max - min + 1) + min - 1
</pre>
To get a random fraction, greater than or equal to zero and less than 1, use this statement:
<pre>
frac! = (rnd(65536)-1)/65536.0
</pre>
To get a random long integer in the range 1 through 2,147,483,647, use this statement:
<pre>
randomInteger& = ((rnd(65536) - 1)<<15) + rnd(32767)
</pre>
 
=={{header|GAP}}==
GAP may use two algorithms : MersenneTwister, or algorithm A in section 3.2.2 of TAOCP (which is the default). One may create several ''random sources'' in parallel, or a global one (based on the TAOCP algorithm).
<langsyntaxhighlight lang="gap"># Creating a random source
rs := RandomSource(IsMersenneTwister);
 
Line 197 ⟶ 461:
 
# Same with default random source
Random(1, 10);</langsyntaxhighlight>
One can get random elements from many objects, including lists
<langsyntaxhighlight lang="gap">
Random([1, 10, 100]);
 
Line 206 ⟶ 470:
 
# Random element of Z/23Z :
Random(Integers mod 23);</langsyntaxhighlight>
 
=={{header|Go}}==
Go has two random number packages providingin randomthe standard library and another package in the numbers"subrepository."
 
# Rand has general purpose random number support. The code attributes the algorithm to DP Mitchell and JA Reeds, and with what little I know, I would guess it's an GFSR. (It uses a large array commented "feeback register" and has variables named "tap" and "feed.")
# [https://golang.org/pkg/math/rand/ math/rand] in the standard library provides general purpose random number support, implementing some sort of feedback shift register. (It uses a large array commented "feeback register" and has variables named "tap" and "feed.") Comments in the code attribute the algorithm to DP Mitchell and JA Reeds. A little more insight is in [https://github.com/golang/go/issues/21835 this issue] in the Go issue tracker.
# Crypto/rand says it "implements a cryptographically secure pseudorandom number generator." I think though it should say that it ''accesses'' a cryptographically secure pseudorandom number generator. It uses /dev/urandom on Linix and similar systems and the CryptGenRandom API on Windows.
# [https://golang.org/pkg/crypto/rand/ crypto/rand], also in the standard library, says it "implements a cryptographically secure pseudorandom number generator." I think though it should say that it ''accesses'' a cryptographically secure pseudorandom number generator. It uses <tt>/dev/urandom</tt> on Unix-like systems and the CryptGenRandom API on Windows.
# [https://godoc.org/golang.org/x/exp/rand x/exp/rand] implements the Permuted Congruential Generator which is also described in the issue linked above.
 
=={{header|Golfscript}}==
Line 224 ⟶ 490:
The [http://www.haskell.org/onlinereport/random.html Haskell 98 report] specifies an interface for pseudorandom number generation and requires that implementations be minimally statistically robust. It is silent, however, on the choice of algorithm.
 
=={{header|Icon}} and {{header|Unicon}} ==
Icon and Unicon both use the same linear congruential random number generator x := (x * 1103515245 + 453816694) mod 2^31. Icon uses an initial seed value of 0 and Unicon randomizes the initial seed.
 
Line 233 ⟶ 499:
=={{header|Inform 7}}==
Inform's random functions are built on the random number generator exposed at runtime by the virtual machine, which is implementation-defined.
 
=={{header|Io}}==
Io's [http://iolanguage.org/scm/io/docs/reference/index.html#/Math/Random/Random Random object] uses the Mersenne Twister algorithm.
 
=={{header|J}}==
Line 242 ⟶ 511:
=={{header|JavaScript}}==
The only built-in random number generation facility is <code>Math.random()</code>, which returns a floating-point number greater than or equal to 0 and less than 1, with approximately uniform distribution. The standard (ECMA-262) does not specify what algorithm is to be used.
 
=={{header|Julia}}==
Julia's [http://docs.julialang.org/en/latest/stdlib/base/#random-numbers built-in random-number generation functions], <code>rand()</code> etcetera, use the Mersenne Twister algorithm.
 
=={{header|Kotlin}}==
As mentioned in the Java entry, the java.util.Random class uses a linear congruential formula and is not therefore cryptographically secure. However, there is also a derived class, java.security.SecureRandom, which can be used for cryptographic purposes
 
=={{header|Lua}}==
Lua's <code>math.random()</code> is an interface to the C <code>rand()</code> function provided by the OS libc; its implementation varies by platform.
 
=={{header|MathematicaM2000 Interpreter}}==
M2000 uses [https://en.wikipedia.org/wiki/Wichmann%E2%80%93Hill Wichmann-Hill Pseudo Random Number Generator]
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Mathematica 7, by default, uses an Extended Cellular Automaton method ("ExtendedCA") to generate random numbers. The main PRNG functions are <code>RandomReal[]</code> and <code>RandomInteger[]</code> You can specify alternative generation methods including the Mersenne Twister and a Linear Congruential Generator (the default earlier versions). Information about random number generation is provided at [http://reference.wolfram.com/mathematica/tutorial/RandomNumberGeneration.html#185956823 Mathematica].
 
=={{header|MATLAB}}==
MATLAB uses the Mersenne Twister as its default random number generator. Information about how the "rand()" function is utilized is given at [http://www.mathworks.com/help/techdoc/ref/rand.html MathWorks].
 
=={{header|Maxima}}==
Maxima uses a Lisp implementation of the Mersenne Twister. See <code>? random</code> for help, or file <code>share/maxima/5.28.0-2/src/rand-mt19937.lisp</code> for the source code.
 
There are also random generators for several [[wp:Probability distribution|distributions]] in package <code>distrib</code> :
* <code>[[wp:Bernoulli distribution|random_bernoulli]]</code>
* <code>[[wp:Beta distribution|random_beta]]</code>
* <code>[[wp:Binomial distribution|random_binomial]]</code>
* <code>[[wp:Cauchy distribution|random_cauchy]]</code>
* <code>[[wp:Chi-squared distribution|random_chi2]]</code>
* <code>[[wp:Uniform distribution (continuous)|random_continuous_uniform]]</code>
* <code>[[wp:Uniform distribution (discrete)|random_discrete_uniform]]</code>
* <code>[[wp:Exponential distribution|random_exp]]</code>
* <code>[[wp:F-distribution|random_f]]</code>
* <code>[[wp:Gamma distribution|random_gamma]]</code>
* <code>[[wp:Categorical distribution|random_general_finite_discrete]]</code>
* <code>[[wp:Geometric distribution|random_geometric]]</code>
* <code>[[wp:Gumbel distribution|random_gumbel]]</code>
* <code>[[wp:Hypergeometric distribution|random_hypergeometric]]</code>
* <code>[[wp:Laplace distribution|random_laplace]]</code>
* <code>[[wp:Logistic distribution|random_logistic]]</code>
* <code>[[wp:Lognormal distribution|random_lognormal]]</code>
* <code>[[wp:Negative binomial distribution|random_negative_binomial]]</code>
* <code>[[wp:Noncentral chi-squared distribution|random_noncentral_chi2]]</code>
* <code>[[wp:Noncentral t-distribution|random_noncentral_student_t]]</code>
* <code>[[wp:Normal distribution|random_normal]]</code>
* <code>[[wp:Pareto distribution|random_pareto]]</code>
* <code>[[wp:Poisson distribution|random_poisson]]</code>
* <code>[[wp:Rayleigh distribution|random_rayleigh]]</code>
* <code>[[wp:Student's t-distribution|random_student_t]]</code>
* <code>[[wp:Weibull distribution|random_weibull]]</code>
 
Note: the package <code>distrib</code> also has functions starting with <code>pdf</code>, <code>cdf</code>, <code>quantile</code>, <code>mean</code>, <code>var</code>, <code>std</code>, <code>skewness</code> or <code>kurtosis</code> instead of <code>random</code>, except the Cauchy distribution, which does not have [[wp:Moment (mathematics)|moments]].
 
=={{header|Modula-3}}==
The Random interface in Modula-3 states that it uses "an additive generator based on Knuth's Algorithm 3.2.2A".
 
=={{header|Nanoquery}}==
The Nanoquery <code>Nanoquery.Util.Random</code> class makes native calls to the [http://java.sun.com/javase/6/docs/api/java/util/Random.html java.util.Random] class, which uses a [[wp:Linear congruential generator|Linear congruential formula]].
 
=={{header|Nemerle}}==
Uses .Net Random class; so, as mentioned under C#, above, implements Knuth's subtractive random number generator algorithm.
Random class documentation at [http://msdn.microsoft.com/en-us/library/system.random.aspx#remarksToggle MSDN].
 
=={{header|NetRexx}}==
As NetRexx runs in the JVM it simply leverages the Java library. See [[#Java|Java]] for details of the algorithms used.
 
=={{header|Nim}}==
There are two PRNGs provided in the standard library:
* '''random''' : Based on xoroshiro128+ (xor/rotate/shift/rotate), see [http://xoroshiro.di.unimi.it/ here].
* '''mersenne''' : The Mersenne Twister.
 
=={{header|OCaml}}==
Line 271 ⟶ 593:
<code>random</code> uses Richard Brent's [http://wwwmaths.anu.edu.au/~brent/random.html xorgens]. It's a member of the xorshift class of PRNGs and provides good, fast pseudorandomness (passing the BigCrush test, unlike the Mersenne twister), but it is not cryptographically strong. As implemented in PARI, its period is "at least <math>2^{4096}-1</math>".
 
<langsyntaxhighlight lang="parigp">setrand(3)
random(6)+1
\\ chosen by fair dice roll.
\\ guaranteed to the random.</langsyntaxhighlight>
 
=={{header|Pascal}}==
See [[#Delphi]] and [[#Free Pascal]].
FreePascal's function random uses the MersenneTwister (for further details, see the file rtl/inc/system.inc)
 
Random functions:
function Random(l: LongInt) : LongInt;
function Random : Real;
procedure Randomize;
 
=={{header|Perl}}==
Previous to Perl 5.20.0 (May 2014), Perl's <code>[http://perldoc.perl.org/functions/rand.html rand]</code> function will try and call <code>[http://www.opengroup.org/onlinepubs/007908775/xsh/drand48.html drand48]</code>, <code>[http://www.opengroup.org/onlinepubs/000095399/functions/random.html random]</code> or <code>[http://www.opengroup.org/onlinepubs/000095399/functions/rand.html rand]</code> from the C library <code>[http://www.opengroup.org/onlinepubs/000095399/basedefs/stdlib.h.html stdlib.h]</code> in that order.
 
Beginning with Perl 5.20.0, a drand48() implementation is built into Perl and used on all platforms. The implementation is from FreeBSD and uses a 48-bit linear congruential generator with this formula:
 
* <math>r_{n + 1} = 25214903917 \times r_n + 11 \pmod {2^{48}}</math>
Seeds for drand48 are 32-bit and the initial seed uses 4 bytes of data read from /dev/urandom if possible; a 32-bit mix of various system values otherwise.
 
Additionally, there are many PRNG's available as modules. Two good Mersenne Twister modules are [https://metacpan.org/pod/Math::Random::MTwist Math::Random::MTwist] and [https://metacpan.org/pod/Math::Random::MT::Auto Math::Random::MT::Auto]. Modules supporting other distributions can be found in [https://metacpan.org/pod/Math::Random Math::Random] and [https://metacpan.org/pod/Math::GSL::Randist Math::GSL::Randist] among others. CSPRNGs include [https://metacpan.org/pod/Bytes::Random::Secure Bytes::Random::Secure], [https://metacpan.org/pod/Math::Random::Secure Math::Random::Secure], [https://metacpan.org/pod/Math::Random::ISAAC Math::Random::ISAAC], and many more.
 
=={{header|Phix}}==
The rand(n) routine returns an integer in the range 1 to n, and rnd() returns a floating point number between 0.0 and 1.0. <br>
In both cases the underlying algorithm is just about as trivial as it can be, certainly not suitable for serious cryptographic work.<br>
There are at least a couple of Mersenne twister components in the archive.
 
=={{header|PHP}}==
Line 296 ⟶ 635:
 
It uses a multiplicative congruential method:
<langsyntaxhighlight PLlang="pl/Ii">seed(x) = mod(950706376 * seed(x-1), 2147483647)
random(x) = seed(x) / 2147483647</langsyntaxhighlight>
 
=={{header|PL/SQL}}==
Line 306 ⟶ 645:
It will automatically initialize with the date, user ID, and process ID if no explicit initialization is performed.
If this package is seeded twice with the same seed, then accessed in the same way, it will produce the same results in both cases.
<langsyntaxhighlight PLlang="pl/SQLsql">DBMS_RANDOM.RANDOM --produces integers in [-2^^31, 2^^31).
DBMS_RANDOM.VALUE --produces numbers in [0,1) with 38 digits of precision.</lang>
DBMS_RANDOM.NORMAL --produces normal distributed numbers with a mean of 0 and a variance of 1</syntaxhighlight>
 
===DBMS_CRYPTO===
Line 314 ⟶ 654:
pseudo-random sequence of bytes, which can be used to generate random material for encryption keys.
This function is based on the RSA X9.31 PRNG (Pseudo-Random Number Generator).
<langsyntaxhighlight PLlang="pl/SQLsql">DBMS_CRYPTO.RANDOMBYTES --returns RAW value
DBMS_CRYPTO.RANDOMINTEGER --produces integers in the BINARY_INTEGER datatype
DBMS_CRYPTO.RANDOMNUMBER --produces integer in the NUMBER datatype in the range of [0..2**128-1]</langsyntaxhighlight>
 
=={{header|PowerShell}}==
Line 326 ⟶ 666:
=={{header|Python}}==
Python uses the [[wp:Mersenne twister|Mersenne twister]] algorithm accessed via the built-in [http://docs.python.org/library/random.html random module].
 
=={{header|Quackery}}==
 
Quackery uses the 64 bit variant of Bob Jenkins' public domain "A small noncryptographic PRNG", which can be found at [https://burtleburtle.net/bob/rand/smallprng.html burtleburtle.net].
 
In case the website does not endure, the C implementation provided is:
 
<syntaxhighlight lang="c">typedef unsigned long long u8;
typedef struct ranctx { u8 a; u8 b; u8 c; u8 d; } ranctx;
 
#define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
u8 ranval( ranctx *x ) {
u8 e = x->a - rot(x->b, 7);
x->a = x->b ^ rot(x->c, 13);
x->b = x->c + rot(x->d, 37);
x->c = x->d + e;
x->d = e + x->a;
return x->d;
}
 
void raninit( ranctx *x, u8 seed ) {
u8 i;
x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
for (i=0; i<20; ++i) {
(void)ranval(x);
}
}</syntaxhighlight>
 
=={{header|R}}==
Line 353 ⟶ 720:
 
See R help on [http://pbil.univ-lyon1.fr/library/base/html/Random.html Random number generation], or in the R system type
<langsyntaxhighlight Rlang="r">?RNG
help.search("Distribution", package="stats")</langsyntaxhighlight>
 
=={{header|Racket}}==
 
Racket's random number generator uses a 54-bit version of L’Ecuyer’s MRG32k3a algorithm [L'Ecuyer02],
as specified in the [http://docs.racket-lang.org/reference/generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._random%29%29 docs].
In addition, the "math" library has a bunch of additional
[http://docs.racket-lang.org/math/base.html#%28part._.Random_.Number_.Generation%29 random functions].
 
=={{header|Raku}}==
(formerly Perl 6)
The implementation underlying the <tt>rand</tt> function is platform and VM dependent. The JVM backend uses that platform's SecureRandom class.
 
=={{header|Rascal}}==
Rascal does not have its own arbitrary number generator, but uses the [[Random_number_generator_(included)#Java | Java]] generator. Nonetheless, you can redefine the arbitrary number generator if needed. Rascal has the following functions connected to the random number generator:
<langsyntaxhighlight lang="rascal">import util::Math;
arbInt(int limit); // generates an arbitrary integer below limit
arbRat(int limit, int limit); // generates an arbitrary rational number between the limits
arbReal(); // generates an arbitrary real value in the interval [0.0, 1.0]
arbSeed(int seed);</langsyntaxhighlight>
The last function can be used to redefine the arbitrary number generator. This function is also used in the getOneFrom() functions.
<langsyntaxhighlight lang="rascal">rascal>import List;
ok
rascal>getOneFrom(["zebra", "elephant", "snake", "owl"]);
str: "owl"
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
The &nbsp; '''random''' &nbsp; BIF function is a pseudo-random number (non-negative integer) generator,
with a range (spread)
<br>limited to &nbsp; 100,000 &nbsp; (but some REXX interpreters support a larger range, including negative numbers).
<br><br>The random numbers generated are not consistent between different REXX interpreters or
<br>even the same REXX interpreters executing on different hardware.
<syntaxhighlight lang="rexx"> /*(below) returns a random integer between 100 & 200, inclusive.*/
 
y = random(100, 200)</syntaxhighlight>
The random numbers may be repeatable by specifiying a &nbsp; ''seed'' &nbsp; for the &nbsp; '''random''' &nbsp; BIF:
<syntaxhighlight lang="rexx">call random ,,44 /*the seed in this case is "44". */
.
.
.
y = random(100, 200)</syntaxhighlight>
 
Comparison of '''random''' BIF output for different REXX implementations using a deterministic ''seed''.
<syntaxhighlight lang="rexx">/* REXX ***************************************************************
* 08.09.2013 Walter Pachl
* Please add the output from other REXXes
* 10.09.2013 Walter Pachl added REXX/TSO
* 01.08.2014 Walter Pachl show what ooRexx supports
**********************************************************************/
Parse Version v
Call random ,,44
ol=v':'
Do i=1 To 10
ol=ol random(1,10)
End
If left(v,11)='REXX-ooRexx' Then
ol=ol random(-999999999,0) /* ooRexx supports negative limits */
Say ol</syntaxhighlight>
'''outputs''' from various REXX interpreters:
<pre>
REXX-ooRexx_4.1.3(MT) 6.03 4 Jul 2013: 3 10 6 8 6 9 9 1 1 6
REXX-ooRexx_4.2.0(MT)_32-bit 6.04 22 Feb 2014: 3 10 6 8 6 9 9 1 1 6 -403019526
REXX/Personal 4.00 21 Mar 1992: 7 7 6 7 8 8 5 9 4 7
REXX-r4 4.00 17 Aug 2013: 8 10 7 5 4 2 10 5 2 4
REXX-roo 4.00 28 Jan 2007: 8 10 7 5 4 2 10 5 2 4
REXX-Regina_3.7(MT) 5.00 14 Oct 2012: 10 2 7 10 1 1 8 2 4 1
REXX-Regina_3.4p1 (temp bug fix sf.org 1898218)(MT) 5.00 21 Feb 2008: 10 2 7 10 1 1 8 2 4 1
REXX-Regina_3.2(MT) 5.00 25 Apr 2003: 10 2 7 10 1 1 8 2 4 1
REXX-Regina_3.3(MT) 5.00 25 Apr 2004: 10 2 7 10 1 1 8 2 4 1
REXX-Regina_3.4(MT) 5.00 30 Dec 2007: 10 2 7 10 1 1 8 2 4 1
REXX-Regina_3.5(MT) 5.00 31 Dec 2009: 10 2 7 10 1 1 8 2 4 1
REXX-Regina_3.6(MT) 5.00 31 Dec 2011: 10 2 7 10 1 1 8 2 4 1
REXX370 3.48 01 May 1992: 8 7 3 1 6 5 5 8 3 2
</pre>
Conclusion: It's not safe to transport a program that uses 'reproducable' use of random-bif (i.e. with a seed) from one environment/implementation to another :-(
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
nr = 10
for i = 1 to nr
see random(i) + nl
next
</syntaxhighlight>
 
=={{header|Ruby}}==
Ruby's <code>rand</code> function currently uses the [[wp:Mersenne twister|Mersenne twister]] algorithm, as described in [http://www.ruby-doc.org/core/classes/Kernel.html#M005974 its documentation].
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">rmd(0)</syntaxhighlight> - Return a pseudorandom value between 0 and 1
 
=={{header|Rust}}==
Rust's <code>[https://crates.io/crates/rand rand]</code> crate offers several PRNGs. (It is also available via <code>#![feature(rustc_private)]</code>). The offering includes some cryptographically secure PRNGs: [https://docs.rs/rand/0.4/rand/isaac/index.html ISAAC] (both 32 and 64-bit variants) and [https://docs.rs/rand/0.4/rand/chacha/struct.ChaChaRng.html ChaCha20]. <code>StdRng</code> is a wrapper of one of those efficient on the current platform. The crate also provides a weak PRNG: [https://docs.rs/rand/0.4/rand/struct.XorShiftRng.html Xorshift128]. It passes diehard but fails TestU01, replacement is being [https://github.com/dhardy/rand/issues/60 considered]. <code>[https://docs.rs/rand/0.4/rand/fn.thread_rng.html thread_rng]</code> returns a thread local <code>StdRng</code> initialized from the OS. Other PRNGs can be created from the OS or with <code>thread_rng</code>.
 
For any other PRNGs not provided, they merely have to implement the <code>[https://docs.rs/rand/0.4/rand/trait.Rng.html Rng]</code> trait.
 
=={{header|Scala}}==
Scala's <code>scala.util.Random</code> class uses a [[wp:Linear congruential generator|Linear congruential formula]] of the JVM run-time libary, as described in [http://java.sun.com/javase/6/docs/api/java/util/Random.html its documentation]. <br>An example can be found here:
<syntaxhighlight lang="scala">import scala.util.Random
 
/**
* Histogram of 200 throws with two dices.
*/
object Throws extends App {
Stream.continually(Random.nextInt(6) + Random.nextInt(6) + 2)
.take(200).groupBy(identity).toList.sortBy(_._1)
.foreach {
case (a, b) => println(f"$a%2d:" + "X" * b.size)
}
}</syntaxhighlight>
{{out}}
<pre> 2:XXX
3:XXXXXXXXX
4:XXXXXXXXXXXXX
5:XXXXXXXXXXXXXXXXXXXXXXXXXX
6:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
8:XXXXXXXXXXXXXXXXXXXXXXXXXXXX
9:XXXXXXXXXXXXXXXXXXXXXXXXXXXX
10:XXXXXXXXXXXXXXXXX
11:XXXXXXXXXXXXXX
12:XX</pre>
 
=={{header|Seed7}}==
Line 381 ⟶ 852:
[http://seed7.sourceforge.net/libraries/bigint.htm#rand%28in_bigInteger,in_bigInteger%29 bigInteger],
[http://seed7.sourceforge.net/libraries/float.htm#rand%28ref_float,ref_float%29 float] and others.
 
=={{header|Sidef}}==
Latest versions of Sidef use the Mersenne Twister algorithm to compute pseudorandom numbers, with different initial seeds (and implementations) for floating-points and integers.
 
<syntaxhighlight lang="ruby">say 1.rand # random float in the interval [0,1)
say 100.irand # random integer in the interval [0,100)</syntaxhighlight>
 
=={{header|Sparkling}}==
Sparkling uses the built-in PRNG of whichever C library implementation the interpreter is compiled against. The Sparkling library functions <tt>random()</tt> and <tt>seed()</tt> map directly to the C standard library functions <tt>rand()</tt> and <tt>srand()</tt> with only one small difference: the return value of <tt>rand()</tt> is divided by <tt>RAND_MAX</tt> so that the generated number is between 0 and 1.
 
=={{header|Standard ML}}==
 
The basis library does not include a random number generator.
 
SML/NJ provides the [https://smlnj.org/doc/smlnj-lib/Util/str-Rand.html Rand] and [https://smlnj.org/doc/smlnj-lib/Util/str-Random.html Random] structures (with a description of the used algorithms in the documentation).
 
MLton additionally ships with [http://mlton.org/MLtonRandom MLtonRandom], which implements a simple LCG, taken from "[https://www.cec.uchile.cl/cinetica/pcordero/MC_libros/NumericalRecipesinC.pdf Numerical Recipes in C]", page 284 ("An Even Quicker Generator").
 
=={{header|Stata}}==
 
See '''[http://www.stata.com/help.cgi?set%20rng set rng]''' in Stata help. Stata uses the '''[https://en.wikipedia.org/wiki/Mersenne_Twister Mersenne Twister]''' RNG by default, and may use the 32-bit '''[https://en.wikipedia.org/wiki/KISS_(algorithm) KISS]''' RNG for compatibility with versions earlier than Stata 14.
 
=={{header|Tcl}}==
Line 389 ⟶ 881:
* W.H. Press & S.A. Teukolsky, “''Portable random number generators'',” Computers in Physics 6(5):522-524, Sep/Oct 1992.
 
=={{header|TI-83 BASIC}}==
TI-83 uses L'Ecuyer's algorithm to generate random numbers.
See [https://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html L'Ecuyer's algorithm].
More explainations can be found in this [http://www.iro.umontreal.ca/~lecuyer/myftp/papers/handstat.pdf paper].
 
Random function:
<syntaxhighlight lang="ti83b">rand</syntaxhighlight>
 
=={{header|TXR}}==
Line 395 ⟶ 894:
=={{header|UNIX Shell}}==
 
All '''Bourne Shell''' clones have a very quick pseudo random number generator.
The Bourne shell does not have an inbuilt random number generator, but calls other programs to generate random numbers, so the algorithm is dependent on the called program. Here is an example of awk being used:
 
<syntaxhighlight lang="bash"> echo $RANDOM </syntaxhighlight>
<lang sh>number=`awk 'BEGIN{print int(rand()*10+1)}'` # Get a random number</lang>
 
Rach time $RANDOM is referenced it changes it's value (with it's maximum value 32767).
 
=={{header|Ursa}}==
Standard Ursa defines the <code>ursa.util.random</code> type for random number generators and gives objects of this type a standard interface, but leaves the choice of algorithm up to the implementor.
 
Cygnus/X Ursa is written in Java and makes calls to java.util.Random, which uses a [[wp:Linear congruential generator|Linear congruential formula]].
 
=={{header|Ursala}}==
Ursala uses the [[wp:Mersenne twister|Mersenne twister]] algorithm as implemented by the [http://www.basis.uklinux.net/avram Avram] run time system for most purposes, except for arbitrary precision floating point random numbers, which are generated by the <code>urandomb</code> function from the
[http://www.mpfr.org mpfr] library.
 
=={{header|V (Vlang)}}==
V (Vlang) has at least two random number modules (at the time this was typed), which are "rand" and "crypto.rand":
 
# https://modules.vlang.io/rand.html, in the standard library, provides two main ways in which users can generate pseudorandom numbers.
# https://modules.vlang.io/crypto.rand.html, also in the standard library, and returns an array of random bytes.
 
=={{header|Wee Basic}}==
Wee Basic does not any built-in algorithms for random number generation. However, as can be seen in [[Random number generator (device)#Wee Basic]], pseudo-random number generation can be accomplished by using a loop that quickly increases the number from 1 to 10 until any key is pressed.
 
=={{header|Wren}}==
Wren's Random class uses the [https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear Well equidistributed long-period linear PRNG (WELL512a)].
 
=={{header|XPL0}}==
Line 412 ⟶ 930:
values modulo the argument.
 
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int I;
[RanSeed(12345); \set random number generator seed to 12345
for I:= 1 to 5 do
[IntOut(0, Ran(1_000_000)); CrLf(0)];
]</langsyntaxhighlight>
 
Output:
Line 428 ⟶ 946:
</pre>
 
=={{header|zkl}}==
zkl uses the Xorshift (http://en.wikipedia.org/wiki/Xorshift) random number generator. It will also, on occasion, read from /dev/urandom.
=={{header|Z80 Assembly}}==
This is a bit of a stretch, but the R register which handles memory refresh can be read from to obtain somewhat random numbers. It only ranges from 0 to 127 and is most likely to be very biased but this is as close to a built-in RNG as the language has.
<syntaxhighlight lang="z80">ld a,r</syntaxhighlight>
=={{header|ZX Spectrum Basic}}==
 
The manual is kind enough to detail how the whole thing works. Nobody is expected to do the maths here, although it has been disassembled online; in short, it's a modified Park-Miller (or [https://en.wikipedia.org/wiki/Lehmer_random_number_generator Lehmer]) generator.
The ZX Spectrum uses a pseudorandom number generator that derives its numbers from a set of values stored in the system ROM. The random numbers produced will repeat after 65535 iterations.
 
Exercises
 
1. Test this rule:
 
Suppose you choose a random number between 1 and 872 and type
<pre>RANDOMIZE your number</pre>
Then the next value of RND will be
<pre>( 75 * ( your number - 1 ) - 1 ) / 65536</pre>
 
2. (For mathematicians only.)
 
Let <math>p</math> be a (large) prime, and let <math>a</math> be a primitive root modulo <math>p</math>.
 
Then if <math>b_i</math> is the residue of <math>a^i</math> modulo <math>p</math> (<math>1 \leq b_i \leq p-1</math>), the sequence
 
<math>\frac{b_i-1}{p-1}</math>
 
is a cyclical sequence of <math>p-1</math> distinct numbers in the range 0 to 1 (excluding 1). By choosing <math>a</math> suitably, these can be made to look fairly random.
 
65537 is a Fermat prime, <math>2^{16}+1</math>. Because the multiplicative group of non-zero residues modulo 65537 has a power of 2 as its order, a residue is a primitive root if and only if it is not a quadratic residue. Use Gauss' law of quadratic reciprocity to show that 75 is a primitive root modulo 65537.
 
The ZX Spectrum uses <math>p</math>=65537 and <math>a</math>=75, and stores some <math>b_i-1</math> in memory. RND entails replacing <math>b_i-1</math> in memory by <math>b_{i+1}-1</math>, and yielding the result <math>(b_{i+1}-1)/(p-1)</math>. RANDOMIZE n (with 1 <math>\leq</math> n <math>\leq</math> 65535) makes <math>b_i</math> equal to n+1.
 
RND is approximately uniformly distributed over the range 0 to 1.
 
{{omit from|6502 Assembly|No RNG}}
{{omit from|68000 Assembly|No RNG}}
{{omit from|8086 Assembly|No RNG}}
{{omit from|bc|No RNG.}}
{{omit from|dc|No RNG.}}
Anonymous user