12 * |
12 * |
13 * This software is provided "AS IS" with no warranty of any kind, |
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 |
14 * express or implied, and with no claim as to its suitability for any |
15 * purpose. |
15 * purpose. |
16 * |
16 * |
|
17 */ |
|
18 |
|
19 /* |
|
20 * This file contains the reimplemented version of the Mersenne Twister |
|
21 * Generator of Matsumoto and Nishimura. |
|
22 * |
|
23 * See the appropriate copyright notice below. |
|
24 * |
|
25 * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
|
26 * All rights reserved. |
|
27 * |
|
28 * Redistribution and use in source and binary forms, with or without |
|
29 * modification, are permitted provided that the following conditions |
|
30 * are met: |
|
31 * |
|
32 * 1. Redistributions of source code must retain the above copyright |
|
33 * notice, this list of conditions and the following disclaimer. |
|
34 * |
|
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. |
|
38 * |
|
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 |
|
41 * permission. |
|
42 * |
|
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. |
|
55 * |
|
56 * |
|
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) |
17 */ |
60 */ |
18 |
61 |
19 #ifndef LEMON_RANDOM_H |
62 #ifndef LEMON_RANDOM_H |
20 #define LEMON_RANDOM_H |
63 #define LEMON_RANDOM_H |
21 |
64 |
395 |
438 |
396 template <typename Word> |
439 template <typename Word> |
397 struct BoolConversion { |
440 struct BoolConversion { |
398 static bool convert(RandomCore<Word>& rnd) { |
441 static bool convert(RandomCore<Word>& rnd) { |
399 return (rnd() & 1) == 1; |
442 return (rnd() & 1) == 1; |
|
443 } |
|
444 }; |
|
445 |
|
446 template <typename Word> |
|
447 struct BoolProducer { |
|
448 Word buffer; |
|
449 int num; |
|
450 |
|
451 BoolProducer() : num(0) {} |
|
452 |
|
453 bool convert(RandomCore<Word>& rnd) { |
|
454 if (num == 0) { |
|
455 buffer = rnd(); |
|
456 num = RandomTraits<Word>::bits; |
|
457 } |
|
458 bool r = (buffer & 1); |
|
459 buffer >>= 1; |
|
460 --num; |
|
461 return r; |
400 } |
462 } |
401 }; |
463 }; |
402 |
464 |
403 } |
465 } |
404 |
466 |
617 return integer<int>(); |
681 return integer<int>(); |
618 } |
682 } |
619 |
683 |
620 /// \brief Returns a random bool |
684 /// \brief Returns a random bool |
621 /// |
685 /// |
622 /// It returns a random bool |
686 /// It returns a random bool. The generator holds a buffer for |
|
687 /// random bits. Every time when it become empty the generator makes |
|
688 /// a new random word and fill the buffer up. |
623 bool boolean() { |
689 bool boolean() { |
624 return _random_bits::BoolConversion<Word>::convert(core); |
690 return bool_producer.convert(core); |
625 } |
691 } |
626 |
692 |
627 ///\name Nonuniform distributions |
693 ///\name Nonuniform distributions |
628 /// |
694 /// |
629 |
695 |