Random number generator (included): Difference between revisions

m
imported>Arakov
 
(10 intermediate revisions by 6 users not shown)
Line 189:
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 208 ⟶ 211:
 
=={{header|Common Lisp}}==
{{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
<syntaxhighlight lang="lisp">(setf *random-state* (make-random-state t))
(randrandom 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 229 ⟶ 231:
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}
Line 278 ⟶ 281:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
Line 284 ⟶ 287:
{
console.printLine(randomGenerator.nextReal());
console.printLine(randomGenerator.evalnextInt(0,100))
}</syntaxhighlight>
{{out}}
Line 374 ⟶ 377:
 
=={{header|Free Pascal}}==
FreePascal's function random uses the MersenneTwister (for further details, see the file rtl/inc/system.inc).
The random is conform MT19937 and is therefor compatible with e.g. the C++11 MT19937 implementation.
 
<syntaxhighlight lang="pascal">
Line 519 ⟶ 520:
=={{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|M2000 Interpreter}}==
M2000 uses [https://en.wikipedia.org/wiki/Wichmann%E2%80%93Hill Wichmann-Hill Pseudo Random Number Generator]
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 905 ⟶ 909:
[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.
Line 949 ⟶ 953:
=={{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.
 
Exercises
 
1. Test this rule:
1. Suppose you choose a random number between 1 and 872 and type
 
1. 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
Line 962 ⟶ 968:
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_ia^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>
Anonymous user