Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/moses-smt/mgiza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen Vermeulen <jtv@precisiontranslationtools.com>2015-07-26 14:59:13 +0300
committerJeroen Vermeulen <jtv@precisiontranslationtools.com>2015-07-26 14:59:13 +0300
commitf5a24feeac441e533f76aa70f4f7d504263d930b (patch)
tree1692ba01ec86e4147f07f6bc90bc02c89f1746bb
parent77cc2dd24dd35319158e23f709a445c6eec92ed6 (diff)
Deal with random numbers outside target interval.
The zufall() ("coincidence," "random chance") function gets a random double in the half-open interval [0, 1), and scales it to fall in the half-open interval [min, max). It asserts that the result is in that interval. But rounding can throw an occasional spanner in the works, and produce e.g. a value that's equal to max. When that happens, just throw the dice again.
-rw-r--r--mgizapp/src/mkcls/general.cpp10
1 files changed, 9 insertions, 1 deletions
diff --git a/mgizapp/src/mkcls/general.cpp b/mgizapp/src/mkcls/general.cpp
index 927142e..5d8cccc 100644
--- a/mgizapp/src/mkcls/general.cpp
+++ b/mgizapp/src/mkcls/general.cpp
@@ -97,7 +97,15 @@ double zufall01()
double zufall(double min,double max)
{
- double z=zufall01()*(max-min)+min;
+ double z;
+ // Get a random double from 0 inclusive to 1 exclusive, and scale to fit
+ // the [min, max) interval. In theory this always produces a number in the
+ // interval, but due to rounding errors it's possible occasionally to get
+ // something just outside it, especially z == max. No point trying to
+ // optimize for that; we can just retry if it happens.
+ do {
+ z = zufall01()*(max-min)+min;
+ } while (z < min || z>= max);
assert(z>=min&&z<max);
return z;
}