Changeset 2372:7fcc0179fb21 in lemon-0.x for lemon/random.h
- Timestamp:
- 02/19/07 20:55:13 (17 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3187
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/random.h
r2356 r2372 15 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 … … 398 441 static bool convert(RandomCore<Word>& rnd) { 399 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 }; … … 461 523 462 524 _random_bits::RandomCore<Word> core; 525 _random_bits::BoolProducer<Word> bool_producer; 526 463 527 464 528 public: … … 620 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 689 bool boolean() { 624 return _random_bits::BoolConversion<Word>::convert(core);690 return bool_producer.convert(core); 625 691 } 626 692
Note: See TracChangeset
for help on using the changeset viewer.