Commit graph

156 commits

Author SHA1 Message Date
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
Joseph Rushton Wakeling
da1dfefba1 Many asserts in std.random can be made static. 2013-06-21 10:49:34 +01:00
Joseph Rushton Wakeling
66cc47b3c1 Remove superflous assert(false) and return statements. 2013-06-21 00:54:37 +01:00
Joseph Rushton Wakeling
0d5162b826 Unittest for MersenneTwisterEngine .save. 2013-06-20 20:34:09 +01:00
Joseph Rushton Wakeling
037ec6e3d1 Complete non-static code coverage for LinearCongruentialEngine unittests.
Unittests for LinearCongruentialEngine were previously missing the
constructor and the .save and .opEquals() methods.

Code coverage analysis still fails to pick up on the static methods
and the contents of a static if scope in lines 374-382 of .popFront().
2013-06-20 20:05:53 +01:00
Joseph Rushton Wakeling
fddc91d5c3 Complete code coverage for Xorshift unittests.
Unittests for Xorshift were previously failing to cover the
constructor, and the .seed(), .save and .opEquals() methods.
2013-06-20 20:02:55 +01:00
Joseph Rushton Wakeling
02fe8ad76a Ensure adequate code coverage from std.random.RandomSample unittests.
The old unittests were missing coverage of large sections of
RandomSample.skip() because the ratios of sample size to source size
were such that Algorithm A was always being chosen over Algorithm D.

The new unittests fix this and also extend coverage to a number of
other areas previously not addressed.  There are also currently-failing
tests commented out with version(none) that can be used to verify
future bugfixes and improvements.

A few lines in skip() remain uncovered simply because they depend on
runtime conditions that are extremely rare.
2013-06-20 19:39:02 +01:00
Joseph Rushton Wakeling
bc386eed72 Spread long statements across several lines. 2013-06-16 15:53:46 +01:00
Joseph Rushton Wakeling
643a2565a1 More formatting corrections. 2013-06-16 15:48:15 +01:00
Joseph Rushton Wakeling
22d26ef8c1 Simplify assert conditions. 2013-06-16 15:26:56 +01:00
Joseph Rushton Wakeling
91b2c664c7 Fix up spacing and brackets for if statements. 2013-06-16 15:21:04 +01:00
Joseph Rushton Wakeling
ca7abe9454 Indentation corrections. :-( 2013-06-09 16:34:05 +02:00
Joseph Rushton Wakeling
a998d02498 RandomSample assertions to ensure skip sizes respect number of items remaining. 2013-06-09 16:18:53 +02:00
Joseph Rushton Wakeling
8da0f13596 Issue 10269 - RandomSample should use popFrontExactly, not popFrontN, when skipping across input range
This small tweak has been accompanied by a couple of extra checks
to ensure that users do not request more sample points than are
available in the input, which could otherwise be the source of
exceptions.

There exists one remaining case where RandomSample may fail: if it
is given an InputRange without the .length property, and the user
indicates that the total number of items available is greater
than what the InputRange actually contains.  In this case an
exception is thrown from std.array.popFront() line 450,
"Attempting to popFront() past the end of an array", or from
std.array.front() line 624, "Attempting to fetch the front of an
empty array."
2013-06-09 15:23:30 +02:00
Joseph Rushton Wakeling
004ce96368 Unittests for randomSample being passed an InputRange.
The assignment of _input = input has been moved from
initialize() to the constructors so as to avoid
problems with nested structs.
2013-06-04 21:54:14 +02:00
Joseph Rushton Wakeling
cbdd14b57b Issue 10265 - RandomSample fails when passed an InputRange as input
* Issue fixed by making presence of save() property conditional
   on input being a ForwardRange.
2013-06-04 19:27:58 +02:00
Walter Bright
a2c9066282 immutable should be an enum 2013-05-25 13:32:53 -07:00
Nils Boßung
4238f5b3ce add an overload for uniform!enum that takes a generator 2013-04-06 23:01:37 +02:00
Timothee Cour
a983cd6d79 Update random.d
fixed spacing
2013-03-12 18:21:47 -07:00