gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Seeding random sequence
0 2 0
default
2 files changed with 28 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -554,48 +554,68 @@
554 554

	
555 555
    /// \brief Copy constructor
556 556
    ///
557 557
    /// Copy constructor. The generated sequence will be identical to
558 558
    /// the other sequence. It can be used to save the current state
559 559
    /// of the generator and later use it to generate the same
560 560
    /// sequence.
561 561
    Random(const Random& other) {
562 562
      core.copyState(other.core);
563 563
    }
564 564

	
565 565
    /// \brief Assign operator
566 566
    ///
567 567
    /// Assign operator. The generated sequence will be identical to
568 568
    /// the other sequence. It can be used to save the current state
569 569
    /// of the generator and later use it to generate the same
570 570
    /// sequence.
571 571
    Random& operator=(const Random& other) {
572 572
      if (&other != this) {
573 573
        core.copyState(other.core);
574 574
      }
575 575
      return *this;
576 576
    }
577 577

	
578
    /// \brief Seeding random sequence
579
    ///
580
    /// Seeding the random sequence. The current number type will be
581
    /// converted to the architecture word type.
582
    template <typename Number>
583
    void seed(Number seed) { 
584
      _random_bits::Initializer<Number, Word>::init(core, seed);
585
    }
586

	
587
    /// \brief Seeding random sequence
588
    ///
589
    /// Seeding the random sequence. The given range should contain
590
    /// any number type and the numbers will be converted to the
591
    /// architecture word type.
592
    template <typename Iterator>
593
    void seed(Iterator begin, Iterator end) { 
594
      typedef typename std::iterator_traits<Iterator>::value_type Number;
595
      _random_bits::Initializer<Number, Word>::init(core, begin, end);
596
    }
597

	
578 598
    /// \brief Returns a random real number from the range [0, 1)
579 599
    ///
580 600
    /// It returns a random real number from the range [0, 1). The
581 601
    /// default Number type is \c double.
582 602
    template <typename Number>
583 603
    Number real() {
584 604
      return _random_bits::RealConversion<Number, Word>::convert(core);
585 605
    }
586 606

	
587 607
    double real() {
588 608
      return real<double>();
589 609
    }
590 610

	
591 611
    /// \brief Returns a random real number the range [0, b)
592 612
    ///
593 613
    /// It returns a random real number from the range [0, b).
594 614
    template <typename Number>
595 615
    Number real(Number b) { 
596 616
      return real<Number>() * b; 
597 617
    }
598 618

	
599 619
    /// \brief Returns a random real number from the range [a, b)
600 620
    ///
601 621
    /// It returns a random real number from the range [a, b).
Ignore white space 48 line context
... ...
@@ -3,35 +3,43 @@
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/random.h>
20 20
#include "test_tools.h"
21 21

	
22 22
///\file \brief Test cases for random.h
23 23
///
24 24
///\todo To be extended
25 25
///
26 26

	
27
int seed_array[] = {1, 2};
28

	
27 29
int main()
28 30
{
29 31
  double a=lemon::rnd();
30 32
  check(a<1.0&&a>0.0,"This should be in [0,1)");
31 33
  a=lemon::rnd.gauss();
32 34
  a=lemon::rnd.gamma(3.45,0);
33 35
  a=lemon::rnd.gamma(4);
34 36
  //Does gamma work with integer k?
35 37
  a=lemon::rnd.gamma(4.0,0);
36 38
  a=lemon::rnd.poisson(.5);
39

	
40
  lemon::rnd.seed(100);
41
  lemon::rnd.seed(seed_array, seed_array + 
42
		  (sizeof(seed_array) / sizeof(seed_array[0])));
43

	
44
  return 0;
37 45
}
0 comments (0 inline)