Commit graph

258 commits

Author SHA1 Message Date
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
Joseph Rushton Wakeling
7c1131318b Add extra seeding check for Mt19937
For amusement, check the first variate of the newly-seeded sequence as
well as the 10_000th.  Both values in this unittest can be verified by
comparison to C++11 by compiling and running the following C++ program:

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

int main ()
{
    static_assert(std::mt19937::default_seed == 5489,
                  "Default seed does not match Phobos!");
    std::mt19937 gen(std::mt19937::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.
2017-02-15 20:47:46 +01:00
Walter Bright
9b3c6524d8 [scope]: annotate std.random 2017-02-08 00:35:08 -08:00
RazvanN7
17e3e642e9 Nitpicks 2016-12-19 16:02:15 +02:00
Sebastian Wilzbach
f0c5a9fad6 Follow-up style fixes for December 2016-12-08 12:32:24 +01:00
Sebastian Wilzbach
2c8166318b Follow-up style fixes for November 2016-12-08 01:46:47 +01:00
Eduard Staniloiu
adb71a6c6b enhancement issue 4851: use assert in choice function 2016-11-14 14:01:52 +02:00
Eduard Staniloiu
783d15bfa0 enhancement issue 4851: add choice function to std.random 2016-11-11 18:09:20 +02:00
Marco Leise
77fd9145e9 Make most random number generator methods @nogc. 2016-09-18 19:42:17 +02:00
Jack Stouffer
0ea4c8c0ef Remove package wide std.algorithm imports from std.random 2016-09-16 10:55:37 +01:00
Atila Neves
3de6bed5c6 Add @system and @safe to std.random unit tests 2016-07-02 11:00:32 +02:00
Jack Stouffer
f6f978b0f7 Fixed local imports in std.random 2016-07-01 12:27:32 -04:00
Sebastian Wilzbach
ec47ac4224 Remove the WEB macro in favor of HTTP
replacement: sed 's/\$(WEB/\$(HTTP/g' -i **/*.d
2016-06-16 00:14:51 +02:00
Sebastian Wilzbach
18b8abf60a remove the deprecated wiki macros 2016-05-27 05:52:23 +02:00
Jack Stouffer
c59a606fd5 Fixed long lines in std/random.d 2016-05-10 20:51:39 -04:00
Sebastian Wilzbach
3d67cd228c style fix: space between operators 2016-04-26 22:26:20 +03:00
Sebastian Wilzbach
780f01d688 std.random: remove upgrading from D1 notice 2016-04-21 02:36:05 +03:00
Sebastian Wilzbach
b05f4afe1c change usage of swapAt to UFCS 2016-04-08 01:18:45 +03:00
Miha Marolt
d22a24a702 Use proper right parenthesis in example's comment: "$(RPAREN)" -> ")" 2016-03-08 18:47:32 +01:00
Joseph Rushton Wakeling
4031f59d34 Remove ddoc references to 'second-generation std.random'
While it would definitely be nice to deliver on this idea, better not to
mention it until we have an actual implementation lined up and approved.
2016-02-26 22:22:13 +01:00
David Herberth
82096b5cf5 RandomSample.front was not documented
The documentation of std.random.RandomSample shows all range primitives except .front
2015-12-17 22:21:31 +01:00
H. S. Teoh
64f43ce42c Merge pull request #3854 from 9rnsr/enforceProp
fix property enforcement
2015-12-15 09:37:12 -08:00
k-hara
e864e53adc fix property enforcement 2015-12-04 15:42:53 +09:00
Jonathan M Davis
26a4340c30 Remove stray uses of TickDuration from Phobos internals. 2015-12-03 03:01:03 -08:00
Vladimir Panteleev
19b0be7de4 fix Issue 15238 - Heisenbug running std.random unittests 2015-10-27 09:44:18 +00:00
Walter Bright
cf22992cbb Merge pull request #3768 from CyberShadow/pull-20151024-051829
HTML fixes
2015-10-24 15:05:00 -07:00
Vladimir Panteleev
86cf380007 HTML fixes 2015-10-24 06:19:23 +00:00
Dragos Carp
d698887729 Remove obsolete TypeTuple references
Replace following names:
std.typetuple      -> std.meta
TypeTuple          -> AliasSeq
ParameterTypeTuple -> Parameters
FieldTypeTuple     -> Fields

std.traits requires more work than search/replace and is left unchanged.
2015-10-13 20:37:44 +02:00
Robert burner Schadek
4f5eaf890e std.random unittest docs
quickfur style
2015-09-10 11:12:56 +02:00
Walter Bright
f9466a982f remove unnecessary uses of std.format 2015-06-30 19:45:15 -07:00
Lionello Lunesu
8fc047f277 Fixed DDoc warnings 2015-05-30 09:17:43 -06:00
Steven Schveighoffer
ae6ec9c2bd Merge pull request #3302 from GassaFM/issue_11738
reverted partialShuffle to match its documentation (issue 11738)
2015-05-21 12:07:28 -04:00
Ivan Kazmenko
e9bb73f184 use a fixed seed 2015-05-21 18:24:12 +03:00
Ivan Kazmenko
7329ef7eae reverted partialShuffle to match its documentation (issue 11738) 2015-05-21 13:25:51 +03:00