Commit graph

291 commits

Author SHA1 Message Date
Jack Stouffer
e14f89e170 Make references to input ranges a link to isInputRange 2018-04-08 16:19:57 -04:00
The Dlang Bot
c1fcea4fdf
Merge pull request #6414 from n8sh/remove-unpredictableseed-from-unittests
Issue 18715 - Non-documented unittests should not use unpredictableSeed or default Random alias
merged-on-behalf-of: Nathan Sashihara <n8sh@users.noreply.github.com>
2018-04-05 01:41:30 +02:00
Nathan Sashihara
c1d1c0e874 Issue 18715 - Non-documented unittests should not use unpredictableSeed or default Random alias 2018-04-04 18:08:00 -04:00
Sebastian Wilzbach
152647e985 Add public examples to std.random 2018-04-04 11:42:56 +02:00
Sebastian Wilzbach
42894784dd Markdownify Phobos
$(D word) -> `word`
2018-04-02 22:32:47 +02:00
Sebastian Wilzbach
72bfff5a74 std.random: add a module-level booktable 2018-03-31 04:33:02 +02:00
Nathan Sashihara
e9f56ba3b2 Fix Issue 18663 - std.random.isSeedable has false positives 2018-03-26 11:45:40 -04:00
The Dlang Bot
4b166f3088
Merge pull request #6331 from jmdavis/revert
Revert fix for 18631.
merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>
2018-03-23 15:51:52 +01:00
The Dlang Bot
cc256d8dc9
Merge pull request #5746 from n8sh/xorshift-fix
Fix Issue 17862 - std.random.XorshiftEngine.min is wrong when bits == 32
merged-on-behalf-of: Andrei Alexandrescu <andralex@users.noreply.github.com>
2018-03-23 01:28:46 +01:00
Jonathan M Davis
5d6d784c9a Revert fix for 18631.
The fix for 18631 broke code. Ranges cannot be marked with const or
inout in generic code. The range API does not require that any functions
be const or inout. In this case, with the changes, choice requires that
length and opSlice be const, breaking any code that uses a range with
choice that does not have a const length or opSlice (which is going to
be a large percentage of ranges with those functions which aren't
arrays).
2018-03-22 18:23:30 -06:00
The Dlang Bot
b87d28f2c7
Merge pull request #6267 from n8sh/unpredictableSeedOf-arc4random
Partially Fix Issue 18596: use arc4random when available for unpredictableSeed
merged-on-behalf-of: unknown
2018-03-22 23:49:27 +01:00
Nathan Sashihara
f39686c771 Partially Fix Issue 18596: use arc4random when available for unpredictableSeed 2018-03-22 03:57:47 -04:00
Basile Burg
8f2ae51cb8 proper usage of inout 2018-03-19 21:25:07 +01:00
Basile Burg
403141166b fix issue 18631 - Allow choice to work with const arrays 2018-03-19 21:02:02 +01:00
The Dlang Bot
d9c94cbb61
Merge pull request #6211 from BBasile/issue-18501
fix issue 18501 - randomShuffle and partialShuffle should return their input argument
merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>
2018-03-02 20:48:36 +01:00
Basile Burg
b9196a976c fix issue 18501 - randomShuffle and partialShuffle should return their input argument 2018-02-23 04:38:58 +01:00
Jack Stouffer
663b5b9278 Revert addition of StdUnittest 2018-02-20 13:32:32 -05:00
Jack Stouffer
18cbb29b04 Replaced version(unittest) blocks with version(StdUnittest) 2018-02-01 19:56:59 -05:00
n8sh
fa92151982 Fix Issue 17862 - std.random.XorshiftEngine.min is wrong when bits == 32
XorshiftEngine.min is defined as 0 regardless of template parameters
but an XorshiftEngine cannot produce a value of zero if its internal
state has the same number of bits as the output element type.
2018-01-29 18:40:24 -05:00
Duncan Paterson
db5c1c84b9 Addresses Issue 17832
Random choice default argument was deciding the type of the
template. I'm not sure if this is a deeper bug, but it can easily be
fixed with this patch which simply overloads the function.
Also added a unittest which would have failed under the old code.
2018-01-28 02:00:54 +01:00
Nathan Sashihara
befc9896d0 Fix Issue 18217: Don't repeatedly call unpredictableSeed to initialize rndGen
Explanation: https://github.com/dlang/phobos/pull/5788#discussion_r146110307
2018-01-09 15:08:11 -05:00
Sebastian Wilzbach
7de3787876 Use static foreach in Phobos 2018-01-03 17:30:11 +01:00
Jack Stouffer
f28bf2d194 Add example to uniform on how to create an array of random numbers 2017-12-29 16:40:09 -05:00
Sebastian Wilzbach
df6365092a Replace body keyword with its replacement: do
Automatic replacement with

    sed -i "s/^\([ ]*\)body/\1do/" -i **/*.d
2017-11-20 15:33:52 +01:00
Martin Nowak
8e17e48db5 Merge remote-tracking branch 'upstream/stable' into merge_stable 2017-10-09 17:14:53 +02:00
Martin
8bd0e26c76 std.random: No equality checks for doc example unittests
They depend on the `Random` alias as well as the `real` precision.
2017-10-08 01:33:46 +02:00
n8sh
fd2e5ba03c Fix Issue 17847 - Properly sanitize seeds for Park–Miller engines
As in XorshiftEngine, sanitizing the seed is more useful than throwing
an exception. The original check was broken because it checked the seed
before taking the modulus. Making this change allows unpredictableSeed
to be marked nothrow and @nogc.
2017-09-21 18:55:42 -04:00
Sebastian Wilzbach
10d05229ae Make Phobos module examples runnable 2017-07-02 00:22:13 +02:00
The Dlang Bot
857fdc1312 Merge pull request #5486 from wilzbach/fix-ddoc
Fix invalid undefined Ddoc macros
merged-on-behalf-of: Vladimir Panteleev <github@thecybershadow.net>
2017-06-15 17:39:23 +02:00
Sebastian Wilzbach
e7111d4fd0 Fix invalid undefined Ddoc macros 2017-06-15 11:00:47 +02:00
Vladimir Panteleev
d0b9555a06
Revert "Sort selective imports"
This reverts commit 998ad51fd7.
2017-06-13 17:51:52 +00:00
Sebastian Wilzbach
998ad51fd7 Sort selective imports 2017-06-12 08:12:09 +02:00
Sebastian Wilzbach
61717ecc7d Sort imports 2017-06-12 07:54:38 +02:00
Ilya Yaroshenko
67e3438c47 Update random.d 2017-04-14 10:16:05 +07:00
Ilya Yaroshenko
91f0544227 fix link
The link is broken because the module name. Hope this will fix it.
2017-04-14 09:18:21 +07:00
H. S. Teoh
ba8ad89b45 Add disclaimer to std.random about unsuitability for cryptographical purposes. 2017-03-27 10:31:21 -07:00
Sebastian Wilzbach
d548e8830a Replace LUCKY links with actual links 2017-02-28 23:46:54 +01:00
Jack Stouffer
4ce5d44dbb Use underscores for number literals with five or more digits 2017-02-23 09:45:49 -05:00
Sebastian Wilzbach
d905ef53b1 Automatically add spaces to binary operators (<=)
command:

sed -E "s/([[:alnum:]]) <= ([[:alnum:]])/\1 <= \2/g" -i **/*.d
sed -E "s/([[:alnum:]])<= ([[:alnum:]])/\1 <= \2/g" -i **/*.d
sed -E "s/([[:alnum:]]) <=([[:alnum:]])/\1 <= \2/g" -i **/*.d
2017-02-23 00:57:47 +01:00
Sebastian Wilzbach
a2c6398332 Automatically add spaces to binary operators (==)
command:

sed -E "s/([[:alnum:]]) == ([[:alnum:]])/\1 == \2/g" -i **/*.d
sed -E "s/([[:alnum:]])== ([[:alnum:]])/\1 == \2/g" -i **/*.d
sed -E "s/([[:alnum:]]) ==([[:alnum:]])/\1 == \2/g" -i **/*.d
2017-02-23 00:57:47 +01:00
Andrei Alexandrescu
30724e67d9 Merge pull request #5166 from wilzbach/dscanner-unittest-safe-or-system
Dscanner: let unittest be @safe or @system
2017-02-22 14:44:08 -05:00
Sebastian Wilzbach
a36cec8686 DScanner: automatially set all unattributed unittests to @safe or @system 2017-02-22 05:42:04 +01:00
Sebastian Wilzbach
425ab667a3 Automatically set the range style from a..b -> a .. b
Commands:

sed -E "s/([[:alnum:]])[.][.]([[:alnum:]])/\1 .. \2/g" -i **/*.d
sed -E "s/([[:alnum:]])[.][.] ([[:alnum:]])/\1 .. \2/g" -i **/*.d
sed -E "s/([[:alnum:]]) [.][.]([[:alnum:]])/\1 .. \2/g" -i **/*.d
2017-02-22 05:37:31 +01:00
Sebastian Wilzbach
805c720595 Unify Phobos by ensuring there's always a space after cast(...)
Command:

sed -E 's/([^"])cast\(([^)]*?)\)([[:alnum:]])/\1cast(\2) \3/g' -i **/*.d
2017-02-21 16:40:20 +01:00
Sebastian Wilzbach
87dec58a41 DStyle: Constraints on declarations should have the same indentation level 2017-02-17 07:36:23 +01:00
Joseph Rushton Wakeling
187bfa27a8 Add test of range-based seeding for Mt19937_64
As with the earlier test of range-based seeding for the 32-bit Mt19937,
this test is added purely in order to ensure consistency of behaviour in
future.  It does not appear to match the behaviour of the superficially
similar sequence-based seeding available in the C++11 <random> version
of the Mersenne Twister.

It does however match the behaviour of the `MersenneTwisterEngine`
implementation available in `hap.random`, which was derived from the
`Boost.Random` implementation, but whose range-based seeding was copied
from Phobos.  This would suggest that any divergence with C++11 is down
entirely to the seeding mechanism.
2017-02-15 21:17:38 +01:00
Joseph Rushton Wakeling
59f1a3d041 Add unittest for Mersenne Twisters with non-standard word size
The word size for `MersenneTwisterEngine` is determined by the template
parameter `w`, not `UIntType`.  For example, a generator with identical
parameters to the standard `Mt19937` but with a `ulong` wordtype should
not produce different results to the standard `uint`-based generator.

This patch adds unittests for the first and 10_000'th values of the
sequences generated by a variety of Mersenne Twister implementations
with non-standard template parameter values.

The values in this unittest can be validated by comparison to C++11 by
compiling and running the following C++ program:

/****************************************************************/

template<class UIntType, size_t w>
void mt_result ()
{
    std::mersenne_twister_engine<
        UIntType, w, 624, 397, 31,
        0x9908b0df, 11, 0xffffffff, 7,
        0x9d2c5680, 15,
        0xefc60000, 18, 1812433253> gen;

    gen.seed(std::mt19937::default_seed);
    std::cout << gen() << std::endl;
    for (int i = 0; i < 9998; ++i)
        gen();
    std::cout << gen() << std::endl;
    std::cout << std::endl;
}

int main ()
{
    mt_result<uint32_t, 32>();
    mt_result<uint64_t, 32>();
    mt_result<uint64_t, 48>();
    mt_result<uint64_t, 64>();
}
/****************************************************************/

Note that the `for` loop in this example advances the generator 9998
times compared to the D unittest's `popFrontN(9999)` because the first
`gen()` call already advances the generator once.
2017-02-15 21:17:38 +01:00
Joseph Rushton Wakeling
45c515f267 Add 64-bit implementation of MersenneTwisterEngine
With the required tempering parameter `d` introduced by the previous
patch, we can now introduce the standard 64-bit implementation of the
Mersenne Twister.  See https://en.wikipedia.org/wiki/Mersenne_Twister
for an explanation of the chosen constants.

Some minimal unittests have been added similar to those already present
for the 32-bit `Mt19937`.  These can be verified by comparison to C++11
by compiling and running the following C++ program:

/****************************************************************/

int main ()
{
    static_assert(std::mt19937_64::default_seed == 5489,
                  "Default seed does not match Phobos!");
    std::mt19937_64 gen(std::mt19937_64::default_seed);
    std::cout << gen() << std::endl;
    for (int i = 0; i < 9998; ++i) {
        gen();
    }
    std::cout << gen() << std::endl;
}
/****************************************************************/

Note that the `for` loop in this example advances the generator 9998
times compared to the D unittest's `popFrontN(9999)` because the first
`gen()` call already advances the generator once.

Fixes Issue #10900 <https://issues.dlang.org/show_bug.cgi?id=10900>.
2017-02-15 21:17:38 +01:00
Joseph Rushton Wakeling
dcd83ac02c Rework MersenneTwisterEngine using Ilya Yaroshenko's faster algorithm
This patch updates MersenneTwisterEngine's implementation to use the new
and much faster implementation Ilya prepared for `mir.random`.

The original Mersenne Twister algorithm (as per the reference C code by
Matsumoto and Nishimura) first regenerates the contents of the entire
state array, before looping over the resulting values and tempering them
in order to generate the actual variates.  Once the entire state array
has been iterated over, the array's values are then regenerated, and so
on and so forth.

Ilya's implementation reworks this idea to intertwine update of a single
entry in the state array with the tempering of another value to produce
the next variate.  This ensures that all the registers concerned stay
'hot' in the CPU's memory and hence ensures significant speedup.

(Just as a mention: as an experiment while adapting this code for phobos
I tried splitting apart the lines responsible for updating the internal
state array from those responsible for tempering the next variate, so
that they could be run sequentially instead of mixed up together; this
resulted in a fairly significant speed hit.)

In contrast to most (all?) other Mersenne Twister implementations, this
one iterates backwards through the state array, which allows for a few
extra compiler optimizations that speed up the algorithm.  It has thus
been necessary to rework the range-based `seed` method to take account
of this.

Besides the algorithmic change, this patch introduces two new template
variables: an extra tempering variable `d`, and the initialization
multiplier `f`, which brings the template implementation in line with
that of the C++11 standard.  These extra template variables are needed
in order to effectively implement the standard 64-bit version of the
Mersenne Twister, which will be added in a follow-up patch.

Finally, this implementation introduces corrections to the handling of
the word size `w` to ensure that the right sequences are produced when
the word size is less than the number of bits in `UIntType`.  Unittests
will be added for this in a follow-up patch.

------------------------------------------------------------------------

Where this implementation differs from that in `mir.random`:

Ilya's original design has been reworked as a drop-in replacement for
the existing Phobos implementation, which is currently implemented as a
forward range (although it should ideally be an input range, but that
design flaw should be fixed as a separate issue).

There appears to be no significant speed hit from reworking Ilya's code
as a range rather than as a functor.  However, some other aspects of the
original design have required rather intrusive changes in order to get
the full speed benefits without introducing breaking change.

The original MersenneTwisterEngine allows for implicit instantiation of
generator instances without providing a seed.  This is handled by
checking in the `front` and `popFront` method whether the generator has
been properly initialized, and seeding it with the default seed if not.
However, these runtime checks on every single call result in a massive
speed hit.  The current implementation therefore takes a number of steps
in order to ensure that the internal state of the generator can be set
to its default-seeded values at compile time:

  * all the internal state variables have been wrapped up in a nested
    `State` struct to facilitate generation;

  * the internals of the `seed` and `popFront` methods have been
    separated out into CTFE'able private static methods (`seedImpl`
    and `popFrontImpl`) which take a reference to a `State` instance
    as input;

  * a CTFE'able private static `defaultState` method has been added
    which generates a `State` instance matching that generated by
    instantiating the generator with the `defaultSeed`.

The `defaultState` method is then used to initialize the default value
of the internal `State` instance at compile-time, replicating the effect
of the original runtime seeding check.

These latter workarounds could be removed in future if the generator
were updated to `@disable this();` and therefore always require explicit
seeding, but this would be a breaking change and so is avoided for now.
2017-02-15 21:17:16 +01:00
Joseph Rushton Wakeling
73e262a9d0 Add test of range-based seeding of Mt19937 random generator
This is added purely in order to verify that generator behaviour will
not change with upcoming code changes.  It does not appear to match the
behaviour of the superficially similar sequence-based seeding available
for the C++11 <random> implementation of the Mersenne Twister.
2017-02-15 20:47:46 +01:00