Commit graph

174 commits

Author SHA1 Message Date
monarch dodra
abcf061a93 Merge pull request #2051 from tom-tan/safe-pure-nothrow-std-random-generators
Mark methods for random number generators in std.random as safe pure not...
2014-04-05 23:32:46 +02:00
Tomoya Tanjo
e520962982 Mark LinearCongruentialEngine.front and XorshiftEngine.front as const 2014-04-04 11:34:37 +09:00
Joseph Rushton Wakeling
93fe844b72 Small optimization to uniform01
Only need to chech u < 1 if RNG returns non-integral variates.
2014-03-27 21:18:40 +01:00
Tomoya Tanjo
412ec99bea Mark methods for random number generators in std.random as safe pure nothrow 2014-03-26 20:19:11 +09:00
Joseph Rushton Wakeling
d0727dc7a0 Fix Issue #5240: faster std.random.uniform for [0.0, 1.0) range
Following typical design in other languages, the function is named
uniform01.  It can be templated on floating-point type, defaulting
to double if none is specified.

The implementation is pretty much derived from its counterpart in
Boost.Random, in particular in its taking account of the different
requirements for integral versus floating-point RNG values, and in
checking that the generated variate is less than 1 before returning.
This last check is necessary simply because we can't guarantee, in
the case of a floating-point based RNG, that we will not get a
result exactly equal to 1.

Fixes http://d.puremagic.com/issues/show_bug.cgi?id=5240
2014-03-25 21:09:41 +01:00
Marc Schütz
90262dcffc Remove another use of the comma operator 2014-03-23 15:51:31 +01:00
Daniel Murphy
4d0892baba Remove uses of the comma operator 2014-02-18 00:48:40 +11:00
Peter Alexander
26eecf4651 Fix Issue 4417 - isUniformRNG constraints for std.random.uniform
https://d.puremagic.com/issues/show_bug.cgi?id=4417
2014-02-15 20:55:56 +00:00
Andrej Mitrovic
749143754a Add uniform example for enums. 2014-02-13 08:18:54 +01:00
k-hara
b391b2ec9f Convert to new alias syntax 2014-02-11 15:27:05 +09:00
Dmitry Olshansky
71f9550eac Merge pull request #1874 from monarchdodra/11960
Fix Issue 11960 - Phobos Mersenne Random Engine not supporting unsigned longs
2014-01-30 11:45:23 -08:00
monarch dodra
fc5ff02eb9 Add an MT unittest 2014-01-22 16:42:49 +01:00
monarch dodra
06e9874060 Fix Issue 11960 - Phobos Mersenne Random Engin...
...e not supporting unsigned longs
2014-01-22 15:11:43 +01:00
monarch dodra
edaad18d00 Document the rest of the parameters 2014-01-22 15:10:04 +01:00
monarch dodra
ffcae1609d Add extra asserts on "w" 2014-01-22 15:08:50 +01:00
monarch dodra
cba9da3835 Move static asserts
So they trigger before they create a compile error
2014-01-22 14:56:12 +01:00
monarch dodra
f680ea030f MT alignement 2014-01-22 14:55:13 +01:00
Daniel Murphy
a656f26e9e Remove use of automatic adjacent string literal concatenation from phobos 2014-01-20 03:42:21 +11:00
monarch dodra
fc48d56284 Merge pull request #1717 from Zshazz/issue_11598
Fix issue 11598 - uniform could be faster for integrals
2013-12-16 05:24:20 -08:00
Joseph Rushton Wakeling
fbc8d1e08e Fix Issue 11738 - partialShuffle should actually _be_ a partial shuffle :-)
This patch fixes the problem that partialShuffle was in fact invariably
shuffling the whole of the input, undetected because there were unittests
only for randomShuffle.

As well as the fix I've added some unittests specifically for partialShuffle
to ensure that it works properly, and I've taken the opportunity to tweak a
couple of bits of Ddoc and tidy up the randomShuffle unittests.
2013-12-14 12:12:33 +01:00
Chris Cain
62f058f11c Fixed trailing whitespace 2013-12-03 07:19:27 -05:00
Chris Cain
3e63ce8a0f Remove special-case check on rare condition
A special case was checked that only occurs when calling uniform with a range of only one element. This was not necessary to keep the math correct and represented a slight slowdown in the common case for no apparent benefit.
2013-12-03 07:16:26 -05:00
Chris Cain
18b8716ff2 Include algorithm documentation for integral uniform 2013-12-02 04:07:05 -05:00
Chris Cain
7c01f661ca Fix formatting issues 2013-12-02 03:58:16 -05:00
Chris Cain
30ced820e5 Fix issue 11598 - uniform could be faster for integrals
These changes retain the uniform quality of this function while, in the
common case, cutting the number of division operations in half.

Additionally, extra unittests were added to assure that all of the
bounds were tested properly.
2013-11-24 23:04:57 -05:00
k-hara
aead6721a6 fix property enforcement 2013-09-28 14:46:44 +09:00
Joseph Rushton Wakeling
ce8155c7ad Fix Issue 10322 - ensure RandomSample is initialized before use
This patch fixes a problem where the public methods .index()
or .popFront() might be called without the first value of the
sample having been determined, which would then cause spurious
results.  The runtime initialization check currently performed
in .front has been extended to those methods.

The private boolean checks in the previous implementation have
been replaced with an enum indicating the algorithm to be used
(A, D or None) with None indicating that the sample has not
been initialized.

Step D1 of Algorithm D has been moved to the skip() function,
which results in a significant performance boost.

Unittests have been introduced to cover the cases where .index
or .popFront() are called before .front.

Finally, the .index method has been made a @property, which I
take to be an oversight of the original code.
2013-09-26 16:53:50 +02:00
Joseph Rushton Wakeling
3bdda604a8 Slightly tone down warning message about value-type RNGs and Random{Cover, Sample}. 2013-08-26 10:51:54 +02:00
Joseph Rushton Wakeling
98a3efb621 Restore Random{Cover, Sample} constructors accepting value-type RNG. 2013-08-26 10:51:54 +02:00
Joseph Rushton Wakeling
691efb167d Make sure RNGs are passed to Random{Cover, Sample} by ref/auto ref. 2013-08-26 10:51:53 +02:00
Joseph Rushton Wakeling
40ee13749f Ensure bool[] _chosen is filled with false at start of cover. 2013-08-26 10:51:53 +02:00
Joseph Rushton Wakeling
c000b794a6 Small stylistic tweaks for Andrei :-) 2013-08-26 10:51:53 +02:00
Joseph Rushton Wakeling
a3ead0ac31 Documentation updates for RandomCover and RandomSample.
Mostly regarding the statistical safety-related concerns
when passing a specified RNG, plus a few corrections.
2013-08-26 10:51:53 +02:00
Joseph Rushton Wakeling
c06ca0ce09 Unittests for RandomCover and RandomSample should test with all RNG types. 2013-08-26 10:51:53 +02:00
Joseph Rushton Wakeling
8e4b77fb3a Correct the .save methods for RandomCover and RandomSample.
RandomCover and RandomSample should only be save'able if:

   * the input is a forward range

   * the thread-global RNG rndGen is not being used

   * the RNG being used is a forward range.
2013-08-26 10:51:53 +02:00
Joseph Rushton Wakeling
2c9ecb8bd1 Partial fix for Issue 10434 - std.random.RandomSample
In line with changes to RandomCover, this patch tweaks
the choice of template parameter and variable names in
order to bring clarity and uniformity to the module.
2013-08-26 10:51:53 +02:00
Joseph Rushton Wakeling
8d9233cf8b Partial fix for Issues 7067 and 10434 - std.random.RandomCover
The existing RandomCover design is fatally flawed because it
requires a RNG as input, which it then copies internally by
value.  So, unless the user is smart enough to pass something
like e.g. SomeRNG(unpredictableSeed), there will be unintended
correlations in random behaviour.

This partial fix follows the design of RandomSample in allowing
RandomCover to use the thread-global default RNG rndGen.  It
also improves the choice of template parameter and variable
names in line with Issue 10434.
2013-08-26 10:51:53 +02:00
Andrei Alexandrescu
73c8b59bb5 Merge pull request #1412 from Hackerpilot/master
Code cleanup
2013-07-19 15:37:56 -07:00
Andrej Mitrovic
443b54e30b Merge pull request #1362 from WebDrake/randomshuffle
Fix Issue 9607 - std.random.randomShuffle() and partialShuffle() don't work with Xorshift.
2013-07-15 15:16:04 -07:00
Hackerpilot
6f9c22bfa9 Merge remote-tracking branch 'upstream/master' 2013-07-13 19:37:11 +00:00
Hackerpilot
ae1dcaa3e1 Removed empty declarations. Converted more C-style array declarations to D-style. Removed trailing whitespace 2013-07-13 19:34:37 +00:00
Joseph Rushton Wakeling
f323100217 Make Xorshift bit-related error messages more informative. 2013-07-09 10:48:06 +02:00
Joseph Rushton Wakeling
b03552d9ac Double safety check for Xorshift bit values. 2013-07-09 09:51:41 +02:00
Joseph Rushton Wakeling
c29e261268 Correct typo in Xorshift160 which was leading to non-uniform behaviour.
The original typo derives directly from George Marsaglia's paper
introducing the Xorshift RNGs.  The original checking values, derived
from that code, thus also reflect the typo.

A corrected version of the reference code is available from
https://github.com/WebDrake/xorshift/ and has been used to
generate the updated checking values.
2013-07-08 22:45:31 +02:00
Joseph Rushton Wakeling
e1bbe0165d Correct typo in Xorshift32 which was leading to non-uniform behaviour.
The original typo derives directly from George Marsaglia's paper
introducing the Xorshift RNGs.  The original checking values, derived
from that code, thus also reflect the typo.

A corrected version of the reference code is available from
https://github.com/WebDrake/xorshift/ and has been used to
generate the updated checking values.
2013-07-08 22:02:34 +02:00
Joseph Rushton Wakeling
ec53fdd5fa Specifically check for 192-bit option in std.random.XorshiftEngine. 2013-07-08 21:55:23 +02:00
Andrej Mitrovic
257399827a Merge pull request #1202 from timotheecour/patch-1
Update random.d
2013-07-05 08:06:48 -07:00
Hackerpilot
13a6e6ac5c Converted C-style array declarations to D-style 2013-07-03 00:06:32 +00:00
Joseph Rushton Wakeling
32b11031a4 Restrict PseudoRngTypes access to package level. 2013-06-22 20:49:52 +01:00
Joseph Rushton Wakeling
e86bc65639 Fix Issue 9607 - std.random.randomShuffle() and partialShuffle()
don't work with Xorshift.

This is an instance of Issue 2803, a clash between a template
parameter and a default argument.  I've used the workaround
proposed in that issue thread:
http://d.puremagic.com/issues/show_bug.cgi?id=2803#c1

Tests have been included to ensure that these functions work
with all possible RNG types.
2013-06-22 09:43:56 +01:00