1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
20 * This file contains the reimplemented version of the Mersenne Twister
21 * Generator of Matsumoto and Nishimura.
23 * See the appropriate copyright notice below.
25 * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
39 * 3. The names of its contributors may not be used to endorse or promote
40 * products derived from this software without specific prior written
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
46 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
47 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54 * OF THE POSSIBILITY OF SUCH DAMAGE.
57 * Any feedback is very welcome.
58 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
59 * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
62 #ifndef LEMON_RANDOM_H
63 #define LEMON_RANDOM_H
71 #include <lemon/math.h>
72 #include <lemon/dim2.h>
77 #include <sys/types.h>
80 #include <lemon/bits/windows.h>
85 ///\brief Mersenne Twister random number generator
89 namespace _random_bits {
91 template <typename _Word, int _bits = std::numeric_limits<_Word>::digits>
92 struct RandomTraits {};
94 template <typename _Word>
95 struct RandomTraits<_Word, 32> {
98 static const int bits = 32;
100 static const int length = 624;
101 static const int shift = 397;
103 static const Word mul = 0x6c078965u;
104 static const Word arrayInit = 0x012BD6AAu;
105 static const Word arrayMul1 = 0x0019660Du;
106 static const Word arrayMul2 = 0x5D588B65u;
108 static const Word mask = 0x9908B0DFu;
109 static const Word loMask = (1u << 31) - 1;
110 static const Word hiMask = ~loMask;
113 static Word tempering(Word rnd) {
115 rnd ^= (rnd << 7) & 0x9D2C5680u;
116 rnd ^= (rnd << 15) & 0xEFC60000u;
123 template <typename _Word>
124 struct RandomTraits<_Word, 64> {
127 static const int bits = 64;
129 static const int length = 312;
130 static const int shift = 156;
132 static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du);
133 static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu);
134 static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u);
135 static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu);
137 static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u);
138 static const Word loMask = (Word(1u) << 31) - 1;
139 static const Word hiMask = ~loMask;
141 static Word tempering(Word rnd) {
142 rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u));
143 rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u));
144 rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u));
151 template <typename _Word>
159 static const int bits = RandomTraits<Word>::bits;
161 static const int length = RandomTraits<Word>::length;
162 static const int shift = RandomTraits<Word>::shift;
167 static const Word seedArray[4] = {
168 0x12345u, 0x23456u, 0x34567u, 0x45678u
171 initState(seedArray, seedArray + 4);
174 void initState(Word seed) {
176 static const Word mul = RandomTraits<Word>::mul;
180 Word *curr = state + length - 1;
181 curr[0] = seed; --curr;
182 for (int i = 1; i < length; ++i) {
183 curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i);
188 template <typename Iterator>
189 void initState(Iterator begin, Iterator end) {
191 static const Word init = RandomTraits<Word>::arrayInit;
192 static const Word mul1 = RandomTraits<Word>::arrayMul1;
193 static const Word mul2 = RandomTraits<Word>::arrayMul2;
196 Word *curr = state + length - 1; --curr;
197 Iterator it = begin; int cnt = 0;
202 num = length > end - begin ? length : end - begin;
204 curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul1))
211 curr = state + length - 1; curr[0] = state[0];
216 num = length - 1; cnt = length - (curr - state) - 1;
218 curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul2))
222 curr = state + length - 1; curr[0] = state[0]; --curr;
227 state[length - 1] = Word(1) << (bits - 1);
230 void copyState(const RandomCore& other) {
231 std::copy(other.state, other.state + length, state);
232 current = state + (other.current - other.state);
236 if (current == state) fillState();
239 return RandomTraits<Word>::tempering(rnd);
246 static const Word mask[2] = { 0x0ul, RandomTraits<Word>::mask };
247 static const Word loMask = RandomTraits<Word>::loMask;
248 static const Word hiMask = RandomTraits<Word>::hiMask;
250 current = state + length;
252 register Word *curr = state + length - 1;
255 num = length - shift;
257 curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
258 curr[- shift] ^ mask[curr[-1] & 1ul];
263 curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
264 curr[length - shift] ^ mask[curr[-1] & 1ul];
267 state[0] = (((state[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
268 curr[length - shift] ^ mask[curr[length - 1] & 1ul];
279 template <typename Result,
280 int shift = (std::numeric_limits<Result>::digits + 1) / 2>
282 static Result mask(const Result& result) {
283 return Masker<Result, (shift + 1) / 2>::
284 mask(static_cast<Result>(result | (result >> shift)));
288 template <typename Result>
289 struct Masker<Result, 1> {
290 static Result mask(const Result& result) {
291 return static_cast<Result>(result | (result >> 1));
295 template <typename Result, typename Word,
296 int rest = std::numeric_limits<Result>::digits, int shift = 0,
297 bool last = rest <= std::numeric_limits<Word>::digits>
298 struct IntConversion {
299 static const int bits = std::numeric_limits<Word>::digits;
301 static Result convert(RandomCore<Word>& rnd) {
302 return static_cast<Result>(rnd() >> (bits - rest)) << shift;
307 template <typename Result, typename Word, int rest, int shift>
308 struct IntConversion<Result, Word, rest, shift, false> {
309 static const int bits = std::numeric_limits<Word>::digits;
311 static Result convert(RandomCore<Word>& rnd) {
312 return (static_cast<Result>(rnd()) << shift) |
313 IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd);
318 template <typename Result, typename Word,
319 bool one_word = (std::numeric_limits<Word>::digits <
320 std::numeric_limits<Result>::digits) >
322 static Result map(RandomCore<Word>& rnd, const Result& bound) {
323 Word max = Word(bound - 1);
324 Result mask = Masker<Result>::mask(bound - 1);
327 num = IntConversion<Result, Word>::convert(rnd) & mask;
333 template <typename Result, typename Word>
334 struct Mapping<Result, Word, false> {
335 static Result map(RandomCore<Word>& rnd, const Result& bound) {
336 Word max = Word(bound - 1);
337 Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2>
347 template <typename Result, int exp>
348 struct ShiftMultiplier {
349 static const Result multiplier() {
350 Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
352 if ((exp & 1) == 1) res *= static_cast<Result>(0.5);
357 template <typename Result>
358 struct ShiftMultiplier<Result, 0> {
359 static const Result multiplier() {
360 return static_cast<Result>(1.0);
364 template <typename Result>
365 struct ShiftMultiplier<Result, 20> {
366 static const Result multiplier() {
367 return static_cast<Result>(1.0/1048576.0);
371 template <typename Result>
372 struct ShiftMultiplier<Result, 32> {
373 static const Result multiplier() {
374 return static_cast<Result>(1.0/4294967296.0);
378 template <typename Result>
379 struct ShiftMultiplier<Result, 53> {
380 static const Result multiplier() {
381 return static_cast<Result>(1.0/9007199254740992.0);
385 template <typename Result>
386 struct ShiftMultiplier<Result, 64> {
387 static const Result multiplier() {
388 return static_cast<Result>(1.0/18446744073709551616.0);
392 template <typename Result, int exp>
394 static Result shift(const Result& result) {
395 return result * ShiftMultiplier<Result, exp>::multiplier();
399 template <typename Result, typename Word,
400 int rest = std::numeric_limits<Result>::digits, int shift = 0,
401 bool last = rest <= std::numeric_limits<Word>::digits>
402 struct RealConversion{
403 static const int bits = std::numeric_limits<Word>::digits;
405 static Result convert(RandomCore<Word>& rnd) {
406 return Shifting<Result, shift + rest>::
407 shift(static_cast<Result>(rnd() >> (bits - rest)));
411 template <typename Result, typename Word, int rest, int shift>
412 struct RealConversion<Result, Word, rest, shift, false> {
413 static const int bits = std::numeric_limits<Word>::digits;
415 static Result convert(RandomCore<Word>& rnd) {
416 return Shifting<Result, shift + bits>::
417 shift(static_cast<Result>(rnd())) +
418 RealConversion<Result, Word, rest-bits, shift + bits>::
423 template <typename Result, typename Word>
426 template <typename Iterator>
427 static void init(RandomCore<Word>& rnd, Iterator begin, Iterator end) {
428 std::vector<Word> ws;
429 for (Iterator it = begin; it != end; ++it) {
430 ws.push_back(Word(*it));
432 rnd.initState(ws.begin(), ws.end());
435 static void init(RandomCore<Word>& rnd, Result seed) {
440 template <typename Word>
441 struct BoolConversion {
442 static bool convert(RandomCore<Word>& rnd) {
443 return (rnd() & 1) == 1;
447 template <typename Word>
448 struct BoolProducer {
452 BoolProducer() : num(0) {}
454 bool convert(RandomCore<Word>& rnd) {
457 num = RandomTraits<Word>::bits;
459 bool r = (buffer & 1);
470 /// \brief Mersenne Twister random number generator
472 /// The Mersenne Twister is a twisted generalized feedback
473 /// shift-register generator of Matsumoto and Nishimura. The period
474 /// of this generator is \f$ 2^{19937} - 1 \f$ and it is
475 /// equi-distributed in 623 dimensions for 32-bit numbers. The time
476 /// performance of this generator is comparable to the commonly used
479 /// This implementation is specialized for both 32-bit and 64-bit
480 /// architectures. The generators differ sligthly in the
481 /// initialization and generation phase so they produce two
482 /// completly different sequences.
484 /// The generator gives back random numbers of serveral types. To
485 /// get a random number from a range of a floating point type you
486 /// can use one form of the \c operator() or the \c real() member
487 /// function. If you want to get random number from the {0, 1, ...,
488 /// n-1} integer range use the \c operator[] or the \c integer()
489 /// method. And to get random number from the whole range of an
490 /// integer type you can use the argumentless \c integer() or \c
491 /// uinteger() functions. After all you can get random bool with
492 /// equal chance of true and false or given probability of true
493 /// result with the \c boolean() member functions.
496 /// // The commented code is identical to the other
497 /// double a = rnd(); // [0.0, 1.0)
498 /// // double a = rnd.real(); // [0.0, 1.0)
499 /// double b = rnd(100.0); // [0.0, 100.0)
500 /// // double b = rnd.real(100.0); // [0.0, 100.0)
501 /// double c = rnd(1.0, 2.0); // [1.0, 2.0)
502 /// // double c = rnd.real(1.0, 2.0); // [1.0, 2.0)
503 /// int d = rnd[100000]; // 0..99999
504 /// // int d = rnd.integer(100000); // 0..99999
505 /// int e = rnd[6] + 1; // 1..6
506 /// // int e = rnd.integer(1, 1 + 6); // 1..6
507 /// int b = rnd.uinteger<int>(); // 0 .. 2^31 - 1
508 /// int c = rnd.integer<int>(); // - 2^31 .. 2^31 - 1
509 /// bool g = rnd.boolean(); // P(g = true) = 0.5
510 /// bool h = rnd.boolean(0.8); // P(h = true) = 0.8
513 /// LEMON provides a global instance of the random number
514 /// generator which name is \ref lemon::rnd "rnd". Usually it is a
515 /// good programming convenience to use this global generator to get
521 typedef unsigned long Word;
523 _random_bits::RandomCore<Word> core;
524 _random_bits::BoolProducer<Word> bool_producer;
529 ///\name Initialization
533 ///\name Initialization
537 /// \brief Default constructor
539 /// Constructor with constant seeding.
540 Random() { core.initState(); }
542 /// \brief Constructor with seed
544 /// Constructor with seed. The current number type will be converted
545 /// to the architecture word type.
546 template <typename Number>
547 Random(Number seed) {
548 _random_bits::Initializer<Number, Word>::init(core, seed);
551 /// \brief Constructor with array seeding
553 /// Constructor with array seeding. The given range should contain
554 /// any number type and the numbers will be converted to the
555 /// architecture word type.
556 template <typename Iterator>
557 Random(Iterator begin, Iterator end) {
558 typedef typename std::iterator_traits<Iterator>::value_type Number;
559 _random_bits::Initializer<Number, Word>::init(core, begin, end);
562 /// \brief Copy constructor
564 /// Copy constructor. The generated sequence will be identical to
565 /// the other sequence. It can be used to save the current state
566 /// of the generator and later use it to generate the same
568 Random(const Random& other) {
569 core.copyState(other.core);
572 /// \brief Assign operator
574 /// Assign operator. The generated sequence will be identical to
575 /// the other sequence. It can be used to save the current state
576 /// of the generator and later use it to generate the same
578 Random& operator=(const Random& other) {
579 if (&other != this) {
580 core.copyState(other.core);
585 /// \brief Seeding random sequence
587 /// Seeding the random sequence. The current number type will be
588 /// converted to the architecture word type.
589 template <typename Number>
590 void seed(Number seed) {
591 _random_bits::Initializer<Number, Word>::init(core, seed);
594 /// \brief Seeding random sequence
596 /// Seeding the random sequence. The given range should contain
597 /// any number type and the numbers will be converted to the
598 /// architecture word type.
599 template <typename Iterator>
600 void seed(Iterator begin, Iterator end) {
601 typedef typename std::iterator_traits<Iterator>::value_type Number;
602 _random_bits::Initializer<Number, Word>::init(core, begin, end);
605 /// \brief Seeding from file or from process id and time
607 /// By default, this function calls the \c seedFromFile() member
608 /// function with the <tt>/dev/urandom</tt> file. If it does not success,
609 /// it uses the \c seedFromTime().
610 /// \return Currently always true.
613 if (seedFromFile("/dev/urandom", 0)) return true;
615 if (seedFromTime()) return true;
619 /// \brief Seeding from file
621 /// Seeding the random sequence from file. The linux kernel has two
622 /// devices, <tt>/dev/random</tt> and <tt>/dev/urandom</tt> which
623 /// could give good seed values for pseudo random generators (The
624 /// difference between two devices is that the <tt>random</tt> may
625 /// block the reading operation while the kernel can give good
626 /// source of randomness, while the <tt>urandom</tt> does not
627 /// block the input, but it could give back bytes with worse
629 /// \param file The source file
630 /// \param offset The offset, from the file read.
631 /// \return True when the seeding successes.
633 bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0)
635 bool seedFromFile(const std::string& file = "", int offset = 0)
638 std::ifstream rs(file.c_str());
641 if (offset != 0 && !rs.seekg(offset)) return false;
642 if (!rs.read(reinterpret_cast<char*>(buf), sizeof(buf))) return false;
643 seed(buf, buf + size);
647 /// \brief Seding from process id and time
649 /// Seding from process id and time. This function uses the
650 /// current process id and the current time for initialize the
652 /// \return Currently always true.
653 bool seedFromTime() {
656 gettimeofday(&tv, 0);
657 seed(getpid() + tv.tv_sec + tv.tv_usec);
659 seed(bits::getWinRndSeed());
666 ///\name Uniform distributions
670 /// \brief Returns a random real number from the range [0, 1)
672 /// It returns a random real number from the range [0, 1). The
673 /// default Number type is \c double.
674 template <typename Number>
676 return _random_bits::RealConversion<Number, Word>::convert(core);
680 return real<double>();
685 ///\name Uniform distributions
689 /// \brief Returns a random real number from the range [0, 1)
691 /// It returns a random double from the range [0, 1).
692 double operator()() {
693 return real<double>();
696 /// \brief Returns a random real number from the range [0, b)
698 /// It returns a random real number from the range [0, b).
699 double operator()(double b) {
700 return real<double>() * b;
703 /// \brief Returns a random real number from the range [a, b)
705 /// It returns a random real number from the range [a, b).
706 double operator()(double a, double b) {
707 return real<double>() * (b - a) + a;
710 /// \brief Returns a random integer from a range
712 /// It returns a random integer from the range {0, 1, ..., b - 1}.
713 template <typename Number>
714 Number integer(Number b) {
715 return _random_bits::Mapping<Number, Word>::map(core, b);
718 /// \brief Returns a random integer from a range
720 /// It returns a random integer from the range {a, a + 1, ..., b - 1}.
721 template <typename Number>
722 Number integer(Number a, Number b) {
723 return _random_bits::Mapping<Number, Word>::map(core, b - a) + a;
726 /// \brief Returns a random integer from a range
728 /// It returns a random integer from the range {0, 1, ..., b - 1}.
729 template <typename Number>
730 Number operator[](Number b) {
731 return _random_bits::Mapping<Number, Word>::map(core, b);
734 /// \brief Returns a random non-negative integer
736 /// It returns a random non-negative integer uniformly from the
737 /// whole range of the current \c Number type. The default result
738 /// type of this function is <tt>unsigned int</tt>.
739 template <typename Number>
741 return _random_bits::IntConversion<Number, Word>::convert(core);
746 unsigned int uinteger() {
747 return uinteger<unsigned int>();
750 /// \brief Returns a random integer
752 /// It returns a random integer uniformly from the whole range of
753 /// the current \c Number type. The default result type of this
754 /// function is \c int.
755 template <typename Number>
757 static const int nb = std::numeric_limits<Number>::digits +
758 (std::numeric_limits<Number>::is_signed ? 1 : 0);
759 return _random_bits::IntConversion<Number, Word, nb>::convert(core);
763 return integer<int>();
766 /// \brief Returns a random bool
768 /// It returns a random bool. The generator holds a buffer for
769 /// random bits. Every time when it become empty the generator makes
770 /// a new random word and fill the buffer up.
772 return bool_producer.convert(core);
777 ///\name Non-uniform distributions
782 /// \brief Returns a random bool
784 /// It returns a random bool with given probability of true result.
785 bool boolean(double p) {
786 return operator()() < p;
789 /// Standard Gauss distribution
791 /// Standard Gauss distribution.
792 /// \note The Cartesian form of the Box-Muller
793 /// transformation is used to generate a random normal distribution.
798 V1=2*real<double>()-1;
799 V2=2*real<double>()-1;
802 return std::sqrt(-2*std::log(S)/S)*V1;
804 /// Gauss distribution with given mean and standard deviation
806 /// Gauss distribution with given mean and standard deviation.
808 double gauss(double mean,double std_dev)
810 return gauss()*std_dev+mean;
813 /// Exponential distribution with given mean
815 /// This function generates an exponential distribution random number
816 /// with mean <tt>1/lambda</tt>.
818 double exponential(double lambda=1.0)
820 return -std::log(1.0-real<double>())/lambda;
823 /// Gamma distribution with given integer shape
825 /// This function generates a gamma distribution random number.
827 ///\param k shape parameter (<tt>k>0</tt> integer)
831 for(int i=0;i<k;i++) s-=std::log(1.0-real<double>());
835 /// Gamma distribution with given shape and scale parameter
837 /// This function generates a gamma distribution random number.
839 ///\param k shape parameter (<tt>k>0</tt>)
840 ///\param theta scale parameter
842 double gamma(double k,double theta=1.0)
845 const double delta = k-std::floor(k);
846 const double v0=E/(E-delta);
848 double V0=1.0-real<double>();
849 double V1=1.0-real<double>();
850 double V2=1.0-real<double>();
853 xi=std::pow(V1,1.0/delta);
854 nu=V0*std::pow(xi,delta-1.0);
861 } while(nu>std::pow(xi,delta-1.0)*std::exp(-xi));
862 return theta*(xi+gamma(int(std::floor(k))));
865 /// Weibull distribution
867 /// This function generates a Weibull distribution random number.
869 ///\param k shape parameter (<tt>k>0</tt>)
870 ///\param lambda scale parameter (<tt>lambda>0</tt>)
872 double weibull(double k,double lambda)
874 return lambda*pow(-std::log(1.0-real<double>()),1.0/k);
877 /// Pareto distribution
879 /// This function generates a Pareto distribution random number.
881 ///\param k shape parameter (<tt>k>0</tt>)
882 ///\param x_min location parameter (<tt>x_min>0</tt>)
884 double pareto(double k,double x_min)
886 return exponential(gamma(k,1.0/x_min))+x_min;
889 /// Poisson distribution
891 /// This function generates a Poisson distribution random number with
892 /// parameter \c lambda.
894 /// The probability mass function of this distribusion is
895 /// \f[ \frac{e^{-\lambda}\lambda^k}{k!} \f]
896 /// \note The algorithm is taken from the book of Donald E. Knuth titled
897 /// ''Seminumerical Algorithms'' (1969). Its running time is linear in the
900 int poisson(double lambda)
902 const double l = std::exp(-lambda);
914 ///\name Two dimensional distributions
919 /// Uniform distribution on the full unit circle
921 /// Uniform distribution on the full unit circle.
923 dim2::Point<double> disc()
927 V1=2*real<double>()-1;
928 V2=2*real<double>()-1;
930 } while(V1*V1+V2*V2>=1);
931 return dim2::Point<double>(V1,V2);
933 /// A kind of two dimensional Gauss distribution
935 /// This function provides a turning symmetric two-dimensional distribution.
936 /// Both coordinates are of standard normal distribution, but they are not
939 /// \note The coordinates are the two random variables provided by
940 /// the Box-Muller method.
941 dim2::Point<double> gauss2()
945 V1=2*real<double>()-1;
946 V2=2*real<double>()-1;
949 double W=std::sqrt(-2*std::log(S)/S);
950 return dim2::Point<double>(W*V1,W*V2);
952 /// A kind of two dimensional exponential distribution
954 /// This function provides a turning symmetric two-dimensional distribution.
955 /// The x-coordinate is of conditionally exponential distribution
956 /// with the condition that x is positive and y=0. If x is negative and
957 /// y=0 then, -x is of exponential distribution. The same is true for the
959 dim2::Point<double> exponential2()
963 V1=2*real<double>()-1;
964 V2=2*real<double>()-1;
967 double W=-std::log(S)/S;
968 return dim2::Point<double>(W*V1,W*V2);