Random number generator (included): Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 35: Line 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]
* [http://vestein.arb-phys.uni-dortmund.de/~wb/RR/rrA5.html 10.5. The particular preludes and postlude - 10.5.1. The particular preludes]
<lang algol68>PROC ℒ next random = (REF ℒ INT a)ℒ REAL: ( a :=
<syntaxhighlight lang="algol68">PROC ℒ next random = (REF ℒ INT a)ℒ REAL: ( a :=
¢ the next pseudo-random ℒ integral value after 'a' from a
¢ the next pseudo-random ℒ integral value after 'a' from a
uniformly distributed sequence on the interval [ℒ 0,ℒ maxint] ¢;
uniformly distributed sequence on the interval [ℒ 0,ℒ maxint] ¢;
Line 46: Line 46:


INT ℒ last random := # some initial random number #;
INT ℒ last random := # some initial random number #;
PROC ℒ random = ℒ REAL: ℒ next random(ℒ last random);</lang>
PROC ℒ random = ℒ REAL: ℒ next random(ℒ last random);</syntaxhighlight>


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;);
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 53: Line 53:


For an ASCII implementation and for '''long real''' precision these routines would appears as:
For an ASCII implementation and for '''long real''' precision these routines would appears as:
<lang algol68>PROC long next random = (REF LONG INT a)LONG REAL: # some suitable next random number #;
<syntaxhighlight 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 #;
INT long last random := # some initial random number #;
PROC long random = LONG REAL: long next random(long last random);</lang>
PROC long random = LONG REAL: long next random(long last random);</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==
Line 113: Line 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.
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.


<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>


Line 125: Line 125:
puts((rand() % 2) ? "heads" : "tails");
puts((rand() % 2) ? "heads" : "tails");
return 0;
return 0;
}</lang>
}</syntaxhighlight>


If the C compiler uses BSD rand(), then this program has only two possible outputs.
If the C compiler uses BSD rand(), then this program has only two possible outputs.
Line 176: Line 176:
Example of use:
Example of use:
{{works with|C++11}}
{{works with|C++11}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <string>
#include <random>
#include <random>
Line 188: Line 188:
std::cout << "Random Number (hardware): " << dist(rd) << std::endl;
std::cout << "Random Number (hardware): " << dist(rd) << std::endl;
std::cout << "Mersenne twister (hardware seeded): " << dist(mt) << std::endl;
std::cout << "Mersenne twister (hardware seeded): " << dist(mt) << std::endl;
}</lang>
}</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 196: Line 196:
CMake has a random ''string'' generator.
CMake has a random ''string'' generator.


<lang cmake># Show random integer from 0 to 9999.
<syntaxhighlight lang="cmake"># Show random integer from 0 to 9999.
string(RANDOM LENGTH 4 ALPHABET 0123456789 number)
string(RANDOM LENGTH 4 ALPHABET 0123456789 number)
math(EXPR number "${number} + 0") # Remove extra leading 0s.
math(EXPR number "${number} + 0") # Remove extra leading 0s.
message(STATUS ${number})</lang>
message(STATUS ${number})</syntaxhighlight>


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''.
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 210: Line 210:
{{incorrect|Common Lisp|Use CLHS as reference. Function rand is incorrect it should be random .}}
{{incorrect|Common Lisp|Use CLHS as reference. Function rand is incorrect it should be random .}}
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
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
<lang lisp>(setf *random-state* (make-random-state t))
<syntaxhighlight lang="lisp">(setf *random-state* (make-random-state t))
(rand 10)</lang>
(rand 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.
[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.


Line 228: Line 228:


Based on the values given in the wikipedia entry here is a Delphi compatible implementation for use in other pascal dialects.
Based on the values given in the wikipedia entry here is a Delphi compatible implementation for use in other pascal dialects.
<lang pascal>
<syntaxhighlight lang="pascal">
{$ifdef fpc}{$mode objfpc}{$endif}
{$ifdef fpc}{$mode objfpc}{$endif}
Line 254: Line 254:
end;
end;
end.</lang>
end.</syntaxhighlight>


=={{header|DWScript}}==
=={{header|DWScript}}==
Line 262: Line 262:
The standard implementation, <code>[[vu]]</code>, uses a Mersenne twister.
The standard implementation, <code>[[vu]]</code>, uses a Mersenne twister.


<lang dejavu>!print random-int # prints a 32-bit random integer</lang>
<syntaxhighlight lang="dejavu">!print random-int # prints a 32-bit random integer</syntaxhighlight>


=={{header|EchoLisp}}==
=={{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].
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 :
Some examples :
<lang lisp>
<syntaxhighlight lang="lisp">
(random-seed "albert")
(random-seed "albert")
(random) → 0.9672510261922906 ; random float in [0 ... 1[
(random) → 0.9672510261922906 ; random float in [0 ... 1[
Line 275: Line 275:
(lib 'bigint)
(lib 'bigint)
(random 1e200) → 48635656441292641677...3917639734865662239925...9490799697903133046309616766848265781368
(random 1e200) → 48635656441292641677...3917639734865662239925...9490799697903133046309616766848265781368
</syntaxhighlight>
</lang>


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 4.x :
ELENA 4.x :
<lang elena>import extensions;
<syntaxhighlight lang="elena">import extensions;
public program()
public program()
Line 285: Line 285:
console.printLine(randomGenerator.nextReal());
console.printLine(randomGenerator.nextReal());
console.printLine(randomGenerator.eval(0,100))
console.printLine(randomGenerator.eval(0,100))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 294: Line 294:
=={{header|Elixir}}==
=={{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:
Elixir does not come with its own module for random number generation. But you can use the appropriate Erlang functions instead. Some examples:
<lang elixir>
<syntaxhighlight lang="elixir">
# Seed the RNG
# Seed the RNG
:random.seed(:erlang.now())
:random.seed(:erlang.now())
Line 303: Line 303:
# Float between 0.0 and 1.0
# Float between 0.0 and 1.0
:random.uniform()
:random.uniform()
</syntaxhighlight>
</lang>
For further information, read the Erlang section.
For further information, read the Erlang section.


Line 316: Line 316:


Seed with a fixed known value triplet A1, A2, A3:
Seed with a fixed known value triplet A1, A2, A3:
<syntaxhighlight lang="erlang">
<lang Erlang>
random:seed(A1, A2, A3)
random:seed(A1, A2, A3)
</syntaxhighlight>
</lang>
Example with the running time:
Example with the running time:
<syntaxhighlight lang="erlang">
<lang Erlang>
...
...
{A1,A2,A3} = erlang:now(),
{A1,A2,A3} = erlang:now(),
Line 327: Line 327:
random:seed(A1, A2, A3),
random:seed(A1, A2, A3),
...same sequence of randoms used
...same sequence of randoms used
</syntaxhighlight>
</lang>
Get a random float value between 0.0 and 1.0:
Get a random float value between 0.0 and 1.0:
<syntaxhighlight lang="erlang">
<lang Erlang>
Rfloat = random:uniform(),
Rfloat = random:uniform(),
</syntaxhighlight>
</lang>
Get a random integer value between 1 and N (N is an integer >= 1):
Get a random integer value between 1 and N (N is an integer >= 1):
<syntaxhighlight lang="erlang">
<lang Erlang>
Rint = random:uniform(N),
Rint = random:uniform(N),
</syntaxhighlight>
</lang>


=={{header|Euler Math Toolbox}}==
=={{header|Euler Math Toolbox}}==
Line 348: Line 348:
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.
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.


<lang fortran>
<syntaxhighlight lang="fortran">
program rosetta_random
program rosetta_random
implicit none
implicit none
Line 371: Line 371:
write(*,'(E24.16)') num
write(*,'(E24.16)') num
end program rosetta_random
end program rosetta_random
</syntaxhighlight>
</lang>


=={{header|Free Pascal}}==
=={{header|Free Pascal}}==
Line 377: Line 377:
The random is conform MT19937 and is therefor compatible with e.g. the C++11 MT19937 implementation.
The random is conform MT19937 and is therefor compatible with e.g. the C++11 MT19937 implementation.


<lang pascal>
<syntaxhighlight lang="pascal">
program RandomNumbers;
program RandomNumbers;
// Program to demonstrate the Random and Randomize functions.
// Program to demonstrate the Random and Randomize functions.
Line 394: Line 394:
Readln;
Readln;
end.
end.
</syntaxhighlight>
</lang>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
Line 453: Line 453:
=={{header|GAP}}==
=={{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).
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).
<lang gap># Creating a random source
<syntaxhighlight lang="gap"># Creating a random source
rs := RandomSource(IsMersenneTwister);
rs := RandomSource(IsMersenneTwister);


Line 460: Line 460:


# Same with default random source
# Same with default random source
Random(1, 10);</lang>
Random(1, 10);</syntaxhighlight>
One can get random elements from many objects, including lists
One can get random elements from many objects, including lists
<lang gap>
<syntaxhighlight lang="gap">
Random([1, 10, 100]);
Random([1, 10, 100]);


Line 469: Line 469:


# Random element of Z/23Z :
# Random element of Z/23Z :
Random(Integers mod 23);</lang>
Random(Integers mod 23);</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
Line 589: Line 589:
<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>".
<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>".


<lang parigp>setrand(3)
<syntaxhighlight lang="parigp">setrand(3)
random(6)+1
random(6)+1
\\ chosen by fair dice roll.
\\ chosen by fair dice roll.
\\ guaranteed to the random.</lang>
\\ guaranteed to the random.</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 631: Line 631:


It uses a multiplicative congruential method:
It uses a multiplicative congruential method:
<lang PL/I>seed(x) = mod(950706376 * seed(x-1), 2147483647)
<syntaxhighlight lang="pl/i">seed(x) = mod(950706376 * seed(x-1), 2147483647)
random(x) = seed(x) / 2147483647</lang>
random(x) = seed(x) / 2147483647</syntaxhighlight>


=={{header|PL/SQL}}==
=={{header|PL/SQL}}==
Line 641: Line 641:
It will automatically initialize with the date, user ID, and process ID if no explicit initialization is performed.
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.
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.
<lang PL/SQL>DBMS_RANDOM.RANDOM --produces integers in [-2^^31, 2^^31).
<syntaxhighlight lang="pl/sql">DBMS_RANDOM.RANDOM --produces integers in [-2^^31, 2^^31).
DBMS_RANDOM.VALUE --produces numbers in [0,1) with 38 digits of precision.
DBMS_RANDOM.VALUE --produces numbers in [0,1) with 38 digits of precision.
DBMS_RANDOM.NORMAL --produces normal distributed numbers with a mean of 0 and a variance of 1</lang>
DBMS_RANDOM.NORMAL --produces normal distributed numbers with a mean of 0 and a variance of 1</syntaxhighlight>


===DBMS_CRYPTO===
===DBMS_CRYPTO===
Line 650: Line 650:
pseudo-random sequence of bytes, which can be used to generate random material for encryption keys.
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).
This function is based on the RSA X9.31 PRNG (Pseudo-Random Number Generator).
<lang PL/SQL>DBMS_CRYPTO.RANDOMBYTES --returns RAW value
<syntaxhighlight lang="pl/sql">DBMS_CRYPTO.RANDOMBYTES --returns RAW value
DBMS_CRYPTO.RANDOMINTEGER --produces integers in the BINARY_INTEGER datatype
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]</lang>
DBMS_CRYPTO.RANDOMNUMBER --produces integer in the NUMBER datatype in the range of [0..2**128-1]</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
Line 669: Line 669:
In case the website does not endure, the C implementation provided is:
In case the website does not endure, the C implementation provided is:


<lang C>typedef unsigned long long u8;
<syntaxhighlight lang="c">typedef unsigned long long u8;
typedef struct ranctx { u8 a; u8 b; u8 c; u8 d; } ranctx;
typedef struct ranctx { u8 a; u8 b; u8 c; u8 d; } ranctx;


Line 688: Line 688:
(void)ranval(x);
(void)ranval(x);
}
}
}</lang>
}</syntaxhighlight>


=={{header|R}}==
=={{header|R}}==
Line 716: Line 716:


See R help on [http://pbil.univ-lyon1.fr/library/base/html/Random.html Random number generation], or in the R system type
See R help on [http://pbil.univ-lyon1.fr/library/base/html/Random.html Random number generation], or in the R system type
<lang R>?RNG
<syntaxhighlight lang="r">?RNG
help.search("Distribution", package="stats")</lang>
help.search("Distribution", package="stats")</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
Line 732: Line 732:
=={{header|Rascal}}==
=={{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:
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:
<lang rascal>import util::Math;
<syntaxhighlight lang="rascal">import util::Math;
arbInt(int limit); // generates an arbitrary integer below limit
arbInt(int limit); // generates an arbitrary integer below limit
arbRat(int limit, int limit); // generates an arbitrary rational number between the limits
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]
arbReal(); // generates an arbitrary real value in the interval [0.0, 1.0]
arbSeed(int seed);</lang>
arbSeed(int seed);</syntaxhighlight>
The last function can be used to redefine the arbitrary number generator. This function is also used in the getOneFrom() functions.
The last function can be used to redefine the arbitrary number generator. This function is also used in the getOneFrom() functions.
<lang rascal>rascal>import List;
<syntaxhighlight lang="rascal">rascal>import List;
ok
ok
rascal>getOneFrom(["zebra", "elephant", "snake", "owl"]);
rascal>getOneFrom(["zebra", "elephant", "snake", "owl"]);
str: "owl"
str: "owl"
</syntaxhighlight>
</lang>


=={{header|REXX}}==
=={{header|REXX}}==
Line 750: Line 750:
<br><br>The random numbers generated are not consistent between different REXX interpreters or
<br><br>The random numbers generated are not consistent between different REXX interpreters or
<br>even the same REXX interpreters executing on different hardware.
<br>even the same REXX interpreters executing on different hardware.
<lang rexx> /*(below) returns a random integer between 100 & 200, inclusive.*/
<syntaxhighlight lang="rexx"> /*(below) returns a random integer between 100 & 200, inclusive.*/


y = random(100, 200)</lang>
y = random(100, 200)</syntaxhighlight>
The random numbers may be repeatable by specifiying a &nbsp; ''seed'' &nbsp; for the &nbsp; '''random''' &nbsp; BIF:
The random numbers may be repeatable by specifiying a &nbsp; ''seed'' &nbsp; for the &nbsp; '''random''' &nbsp; BIF:
<lang rexx>call random ,,44 /*the seed in this case is "44". */
<syntaxhighlight lang="rexx">call random ,,44 /*the seed in this case is "44". */
.
.
.
.
.
.
y = random(100, 200)</lang>
y = random(100, 200)</syntaxhighlight>


Comparison of '''random''' BIF output for different REXX implementations using a deterministic ''seed''.
Comparison of '''random''' BIF output for different REXX implementations using a deterministic ''seed''.
<lang rexx>/* REXX ***************************************************************
<syntaxhighlight lang="rexx">/* REXX ***************************************************************
* 08.09.2013 Walter Pachl
* 08.09.2013 Walter Pachl
* Please add the output from other REXXes
* Please add the output from other REXXes
Line 775: Line 775:
If left(v,11)='REXX-ooRexx' Then
If left(v,11)='REXX-ooRexx' Then
ol=ol random(-999999999,0) /* ooRexx supports negative limits */
ol=ol random(-999999999,0) /* ooRexx supports negative limits */
Say ol</lang>
Say ol</syntaxhighlight>
'''outputs''' from various REXX interpreters:
'''outputs''' from various REXX interpreters:
<pre>
<pre>
Line 795: Line 795:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
nr = 10
nr = 10
for i = 1 to nr
for i = 1 to nr
see random(i) + nl
see random(i) + nl
next
next
</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 806: Line 806:


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>rmd(0)</lang> - Return a pseudorandom value between 0 and 1
<syntaxhighlight lang="runbasic">rmd(0)</syntaxhighlight> - Return a pseudorandom value between 0 and 1


=={{header|Rust}}==
=={{header|Rust}}==
Line 815: Line 815:
=={{header|Scala}}==
=={{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:
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:
<lang scala>import scala.util.Random
<syntaxhighlight lang="scala">import scala.util.Random


/**
/**
Line 826: Line 826:
case (a, b) => println(f"$a%2d:" + "X" * b.size)
case (a, b) => println(f"$a%2d:" + "X" * b.size)
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 2:XXX
<pre> 2:XXX
Line 852: Line 852:
Latest versions of Sidef use the Mersenne Twister algorithm to compute pseudorandom numbers, with different initial seeds (and implementations) for floating-points and integers.
Latest versions of Sidef use the Mersenne Twister algorithm to compute pseudorandom numbers, with different initial seeds (and implementations) for floating-points and integers.


<lang ruby>say 1.rand # random float in the interval [0,1)
<syntaxhighlight lang="ruby">say 1.rand # random float in the interval [0,1)
say 100.irand # random integer in the interval [0,100)</lang>
say 100.irand # random integer in the interval [0,100)</syntaxhighlight>


=={{header|Sparkling}}==
=={{header|Sparkling}}==
Line 883: Line 883:


Random function:
Random function:
<lang ti83b>rand</lang>
<syntaxhighlight lang="ti83b">rand</syntaxhighlight>


=={{header|TXR}}==
=={{header|TXR}}==
Line 892: Line 892:
All '''Bourne Shell''' clones have a very quick pseudo random number generator.
All '''Bourne Shell''' clones have a very quick pseudo random number generator.


<lang bash> echo $RANDOM </lang>
<syntaxhighlight lang="bash"> echo $RANDOM </syntaxhighlight>


Rach time $RANDOM is referenced it changes it's value (with it's maximum value 32767).
Rach time $RANDOM is referenced it changes it's value (with it's maximum value 32767).
Line 926: Line 926:
values modulo the argument.
values modulo the argument.


<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int I;
int I;
[RanSeed(12345); \set random number generator seed to 12345
[RanSeed(12345); \set random number generator seed to 12345
for I:= 1 to 5 do
for I:= 1 to 5 do
[IntOut(0, Ran(1_000_000)); CrLf(0)];
[IntOut(0, Ran(1_000_000)); CrLf(0)];
]</lang>
]</syntaxhighlight>


Output:
Output:
Line 946: Line 946:
=={{header|Z80 Assembly}}==
=={{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.
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.
<lang z80>ld a,r</lang>
<syntaxhighlight lang="z80">ld a,r</syntaxhighlight>
=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==