This file is indexed.

/usr/include/vigra/random.hxx is in libvigraimpex-dev 1.10.0+dfsg-11ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
/************************************************************************/
/*                                                                      */
/*                  Copyright 2008 by Ullrich Koethe                    */
/*                                                                      */
/*    This file is part of the VIGRA computer vision library.           */
/*    The VIGRA Website is                                              */
/*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
/*    Please direct questions, bug reports, and contributions to        */
/*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
/*        vigra@informatik.uni-hamburg.de                               */
/*                                                                      */
/*    Permission is hereby granted, free of charge, to any person       */
/*    obtaining a copy of this software and associated documentation    */
/*    files (the "Software"), to deal in the Software without           */
/*    restriction, including without limitation the rights to use,      */
/*    copy, modify, merge, publish, distribute, sublicense, and/or      */
/*    sell copies of the Software, and to permit persons to whom the    */
/*    Software is furnished to do so, subject to the following          */
/*    conditions:                                                       */
/*                                                                      */
/*    The above copyright notice and this permission notice shall be    */
/*    included in all copies or substantial portions of the             */
/*    Software.                                                         */
/*                                                                      */
/*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
/*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
/*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
/*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
/*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
/*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
/*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
/*    OTHER DEALINGS IN THE SOFTWARE.                                   */
/*                                                                      */
/************************************************************************/


#ifndef VIGRA_RANDOM_HXX
#define VIGRA_RANDOM_HXX

#include "mathutil.hxx"
#include "functortraits.hxx"
#include "array_vector.hxx"

#include <ctime>

    // includes to get the current process and thread IDs
    // to be used for automated seeding
#ifdef _MSC_VER
  #include <vigra/windows.h>  // for GetCurrentProcessId() and GetCurrentThreadId()
#endif

#if __linux__
  #include <unistd.h>       // for getpid()
  #include <sys/syscall.h>  // for SYS_gettid
#endif

#if __APPLE__
  #include <unistd.h>               // for getpid()
  #include <sys/syscall.h>          // SYS_thread_selfid
  #include <AvailabilityMacros.h>   // to check if we are on MacOS 10.6 or later
#endif

namespace vigra {

enum RandomSeedTag { RandomSeed };

namespace detail {

enum RandomEngineTag { TT800, MT19937 };


template<RandomEngineTag EngineTag>
struct RandomState;

template <RandomEngineTag EngineTag>
void seed(UInt32 theSeed, RandomState<EngineTag> & engine)
{
    engine.state_[0] = theSeed;
    for(UInt32 i=1; i<RandomState<EngineTag>::N; ++i)
    {
        engine.state_[i] = 1812433253U * (engine.state_[i-1] ^ (engine.state_[i-1] >> 30)) + i;
    }
}

template <class Iterator, RandomEngineTag EngineTag>
void seed(Iterator init, UInt32 key_length, RandomState<EngineTag> & engine)
{
    const UInt32 N = RandomState<EngineTag>::N;
    int k = (int)std::max(N, key_length);
    UInt32 i = 1, j = 0;
    Iterator data = init;
    for (; k; --k) 
    {
        engine.state_[i] = (engine.state_[i] ^ ((engine.state_[i-1] ^ (engine.state_[i-1] >> 30)) * 1664525U))
                           + *data + j; /* non linear */
        ++i; ++j; ++data;
        
        if (i >= N) 
        { 
            engine.state_[0] = engine.state_[N-1]; 
            i=1; 
        }
        if (j>=key_length)
        { 
            j=0;
            data = init;
        }
    }

    for (k=N-1; k; --k) 
    {
        engine.state_[i] = (engine.state_[i] ^ ((engine.state_[i-1] ^ (engine.state_[i-1] >> 30)) * 1566083941U))
                           - i; /* non linear */
        ++i;
        if (i>=N) 
        { 
            engine.state_[0] = engine.state_[N-1]; 
            i=1; 
        }
    }

    engine.state_[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */ 
}

template <RandomEngineTag EngineTag>
void seed(RandomSeedTag, RandomState<EngineTag> & engine)
{
    static UInt32 globalCount = 0;
    ArrayVector<UInt32> seedData;
    
    seedData.push_back((UInt32)time(0));
    seedData.push_back((UInt32)clock());
    seedData.push_back(++globalCount);
    
    std::size_t ptr((char*)&engine - (char*)0);
    seedData.push_back((UInt32)(ptr & 0xffffffff));
    static const UInt32 shift = sizeof(ptr) > 4 ? 32 : 16;
    seedData.push_back((UInt32)(ptr >> shift));
    
#ifdef _MSC_VER
    seedData.push_back((UInt32)GetCurrentProcessId());
    seedData.push_back((UInt32)GetCurrentThreadId());
#endif

#ifdef __linux__
    seedData.push_back((UInt32)getpid());
# ifdef SYS_gettid
    seedData.push_back((UInt32)syscall(SYS_gettid));
# endif
#endif

#ifdef __APPLE__
    seedData.push_back((UInt32)getpid());
  #if defined(SYS_thread_selfid) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6)
    // SYS_thread_selfid was introduced in MacOS 10.6
    seedData.push_back((UInt32)syscall(SYS_thread_selfid));
  #endif
#endif

    seed(seedData.begin(), seedData.size(), engine);
}

    /* Tempered twister TT800 by M. Matsumoto */
template<>
struct RandomState<TT800>
{
    static const UInt32 N = 25, M = 7;
    
    mutable UInt32 state_[N];
    mutable UInt32 current_;
                   
    RandomState()
    : current_(0)
    {
        UInt32 seeds[N] = { 
            0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23,
            0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825,
            0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,
            0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9,
            0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb
        };
         
        for(UInt32 i=0; i<N; ++i)
            state_[i] = seeds[i];
    }

  protected:  

    UInt32 get() const
    {
        if(current_ == N)
            generateNumbers<void>();
            
        UInt32 y = state_[current_++];
        y ^= (y << 7) & 0x2b5b2500; 
        y ^= (y << 15) & 0xdb8b0000; 
        return y ^ (y >> 16);
    }
    
    template <class DUMMY>
    void generateNumbers() const;

    void seedImpl(RandomSeedTag)
    {
        seed(RandomSeed, *this);
    }

    void seedImpl(UInt32 theSeed)
    {
        seed(theSeed, *this);
    }
    
    template<class Iterator>
    void seedImpl(Iterator init, UInt32 length)
    {
        seed(init, length, *this);
    }
};

template <class DUMMY>
void RandomState<TT800>::generateNumbers() const
{
    UInt32 mag01[2]= { 0x0, 0x8ebfd028 };

    for(UInt32 i=0; i<N-M; ++i)
    {
        state_[i] = state_[i+M] ^ (state_[i] >> 1) ^ mag01[state_[i] % 2];
    }
    for (UInt32 i=N-M; i<N; ++i) 
    {
        state_[i] = state_[i+(M-N)] ^ (state_[i] >> 1) ^ mag01[state_[i] % 2];
    }
    current_ = 0;
}

    /* Mersenne twister MT19937 by M. Matsumoto */
template<>
struct RandomState<MT19937>
{
    static const UInt32 N = 624, M = 397;
    
    mutable UInt32 state_[N];
    mutable UInt32 current_;
                   
    RandomState()
    : current_(0)
    {
        seed(19650218U, *this);
    }

  protected:  

    UInt32 get() const
    {
        if(current_ == N)
            generateNumbers<void>();
            
        UInt32 x = state_[current_++];
        x ^= (x >> 11);
        x ^= (x << 7) & 0x9D2C5680U;
        x ^= (x << 15) & 0xEFC60000U;
        return x ^ (x >> 18);
    }
    
    template <class DUMMY>
    void generateNumbers() const;

    static UInt32 twiddle(UInt32 u, UInt32 v) 
    {
        return (((u & 0x80000000U) | (v & 0x7FFFFFFFU)) >> 1)
                ^ ((v & 1U) ? 0x9908B0DFU : 0x0U);
    }

    void seedImpl(RandomSeedTag)
    {
        seed(RandomSeed, *this);
        generateNumbers<void>();
    }

    void seedImpl(UInt32 theSeed)
    {
        seed(theSeed, *this);
        generateNumbers<void>();
    }
    
    template<class Iterator>
    void seedImpl(Iterator init, UInt32 length)
    {
        seed(19650218U, *this);
        seed(init, length, *this);
        generateNumbers<void>();
    }
};

template <class DUMMY>
void RandomState<MT19937>::generateNumbers() const
{
    for (unsigned int i = 0; i < (N - M); ++i)
    {
        state_[i] = state_[i + M] ^ twiddle(state_[i], state_[i + 1]);
    }
    for (unsigned int i = N - M; i < (N - 1); ++i)
    {
        state_[i] = state_[i + M - N] ^ twiddle(state_[i], state_[i + 1]);
    }
    state_[N - 1] = state_[M - 1] ^ twiddle(state_[N - 1], state_[0]);
    current_ = 0;
}

} // namespace detail


/** \addtogroup RandomNumberGeneration Random Number Generation

     High-quality random number generators and related functors.
*/
//@{

/** Generic random number generator.

    The actual generator is passed in the template argument <tt>Engine</tt>. Two generators
    are currently available:
    <ul>
    <li> <tt>RandomMT19937</tt>: The state-of-the-art <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html">Mersenne Twister</a> with a state length of 2<sup>19937</sup> and very high statistical quality.
    <li> <tt>RandomTT800</tt>: (default) The Tempered Twister, a simpler predecessor of the Mersenne Twister with period length 2<sup>800</sup>.
    </ul>
    
    Both generators have been designed by <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/eindex.html">Makoto Matsumoto</a>. 
    
    <b>Traits defined:</b>
    
    \verbatim FunctorTraits<RandomNumberGenerator<Engine> >::isInitializer \endverbatim
    is true (<tt>VigraTrueType</tt>).
*/
template <class Engine = detail::RandomState<detail::MT19937> >
class RandomNumberGenerator
: public Engine
{
    mutable double normalCached_;
    mutable bool normalCachedValid_;
    
  public:
  
        /** Create a new random generator object with standard seed.
            
            Due to standard seeding, the random numbers generated will always be the same. 
            This is useful for debugging.
        */
    RandomNumberGenerator()
    : normalCached_(0.0),
      normalCachedValid_(false)
    {}
  
        /** Create a new random generator object with a random seed.
        
            The seed is obtained from the machines current <tt>clock()</tt> and <tt>time()</tt>
            values.
        
            <b>Usage:</b>
            \code
            RandomNumberGenerator<> rnd = RandomNumberGenerator<>(RandomSeed);
            \endcode
        */
    RandomNumberGenerator(RandomSeedTag)
    : normalCached_(0.0),
      normalCachedValid_(false)
    {
        this->seedImpl(RandomSeed);
    }
  
        /** Create a new random generator object from the given seed.
            
            The same seed will always produce identical random sequences.
            If \a ignoreSeed is <tt>true</tt>, the given seed is ignored,
            and the generator is seeded randomly (as if it was constructed 
            with <tt>RandomNumberGenerator<>(RandomSeed)</tt>). This allows 
            you to switch between random and deterministic seeding at
            run-time.
        */
    RandomNumberGenerator(UInt32 theSeed, bool ignoreSeed=false)
    : normalCached_(0.0),
      normalCachedValid_(false)
    {
        if(ignoreSeed)
            this->seedImpl(RandomSeed);
        else
            this->seedImpl(theSeed);
    }

        /** Create a new random generator object from the given seed sequence.
            
            Longer seed sequences lead to better initialization in the sense that the generator's 
            state space is covered much better than is possible with 32-bit seeds alone.
        */
    template<class Iterator>
    RandomNumberGenerator(Iterator init, UInt32 length)
    : normalCached_(0.0),
      normalCachedValid_(false)
    {
        this->seedImpl(init, length);
    }

  
        /** Re-initialize the random generator object with a random seed.
        
            The seed is obtained from the machines current <tt>clock()</tt> and <tt>time()</tt>
            values.
        
            <b>Usage:</b>
            \code
            RandomNumberGenerator<> rnd = ...;
            ...
            rnd.seed(RandomSeed);
            \endcode
        */
    void seed(RandomSeedTag)
    {
        this->seedImpl(RandomSeed);
        normalCachedValid_ = false;
    }

        /** Re-initialize the random generator object from the given seed.
            
            The same seed will always produce identical random sequences.
            If \a ignoreSeed is <tt>true</tt>, the given seed is ignored,
            and the generator is seeded randomly (as if <tt>seed(RandomSeed)</tt>
            was called). This allows you to switch between random and deterministic 
            seeding at run-time.
        */
    void seed(UInt32 theSeed, bool ignoreSeed=false)
    {
        if(ignoreSeed)
            this->seedImpl(RandomSeed);
        else
            this->seedImpl(theSeed);
        normalCachedValid_ = false;
    }

        /** Re-initialize the random generator object from the given seed sequence.
            
            Longer seed sequences lead to better initialization in the sense that the generator's 
            state space is covered much better than is possible with 32-bit seeds alone.
        */
    template<class Iterator>
    void seed(Iterator init, UInt32 length)
    {
        this->seedImpl(init, length);
        normalCachedValid_ = false;
    }

        /** Return a uniformly distributed integer random number in [0, 2<sup>32</sup>).
            
            That is, 0 &lt;= i &lt; 2<sup>32</sup>. 
        */
    UInt32 operator()() const
    {
        return this->get();
    }

        /** Return a uniformly distributed integer random number in [0, 2<sup>32</sup>).
            
            That is, 0 &lt;= i &lt; 2<sup>32</sup>. 
        */
    UInt32 uniformInt() const
    {
        return this->get();
    }


#if 0 // difficult implementation necessary if low bits are not sufficiently random
        // in [0,beyond)
    UInt32 uniformInt(UInt32 beyond) const
    {
        if(beyond < 2)
            return 0;

        UInt32 factor = factorForUniformInt(beyond);
        UInt32 res = this->get() / factor;

        // Use rejection method to avoid quantization bias.
        // On average, we will need two raw random numbers to generate one.
        while(res >= beyond)
            res = this->get() / factor;
        return res;
    }
#endif /* #if 0 */

        /** Return a uniformly distributed integer random number in [0, <tt>beyond</tt>).
            
            That is, 0 &lt;= i &lt; <tt>beyond</tt>. 
        */
    UInt32 uniformInt(UInt32 beyond) const
    {
        // in [0,beyond) -- simple implementation when low bits are sufficiently random, which is
        // the case for TT800 and MT19937
        if(beyond < 2)
            return 0;

        UInt32 remainder = (NumericTraits<UInt32>::max() - beyond + 1) % beyond;
        UInt32 lastSafeValue = NumericTraits<UInt32>::max() - remainder;
        UInt32 res = this->get();

        // Use rejection method to avoid quantization bias.
        // We will need two raw random numbers in amortized worst case.
        while(res > lastSafeValue)
            res = this->get();
        return res % beyond;
    }
    
        /** Return a uniformly distributed double-precision random number in [0.0, 1.0).
            
            That is, 0.0 &lt;= i &lt; 1.0. All 53-bit bits of the mantissa are random (two 32-bit integers are used to 
            create this number).
        */
    double uniform53() const
    {
        // make full use of the entire 53-bit mantissa of a double, by Isaku Wada
        return ( (this->get() >> 5) * 67108864.0 + (this->get() >> 6)) * (1.0/9007199254740992.0); 
    }
    
        /** Return a uniformly distributed double-precision random number in [0.0, 1.0].
            
            That is, 0.0 &lt;= i &lt;= 1.0. This number is computed by <tt>uniformInt()</tt> / (2<sup>32</sup> - 1), 
            so it has effectively only 32 random bits. 
        */
    double uniform() const
    {
        return (double)this->get() / 4294967295.0;
    }

        /** Return a uniformly distributed double-precision random number in [lower, upper].
           
            That is, <tt>lower</tt> &lt;= i &lt;= <tt>upper</tt>. This number is computed 
            from <tt>uniform()</tt>, so it has effectively only 32 random bits. 
        */
    double uniform(double lower, double upper) const
    {
        vigra_precondition(lower < upper,
          "RandomNumberGenerator::uniform(): lower bound must be smaller than upper bound."); 
        return uniform() * (upper-lower) + lower;
    }

        /** Return a standard normal variate (Gaussian) random number.
           
            Mean is zero, standard deviation is 1.0. It uses the polar form of the 
            Box-Muller transform.
        */
    double normal() const;
    
        /** Return a normal variate (Gaussian) random number with the given mean and standard deviation.
           
            It uses the polar form of the Box-Muller transform.
        */
    double normal(double mean, double stddev) const
    {
        vigra_precondition(stddev > 0.0,
          "RandomNumberGenerator::normal(): standard deviation must be positive."); 
        return normal()*stddev + mean;
    }
    
        /** Access the global (program-wide) instance of the present random number generator.
        
            Normally, you will create a local generator by one of the constructor calls. But sometimes
            it is useful to have all program parts access the same generator.
        */
    static RandomNumberGenerator & global()
    {
        return global_;
    }

    static UInt32 factorForUniformInt(UInt32 range)
    {
        return (range > 2147483648U || range == 0)
                     ? 1
                     : 2*(2147483648U / ceilPower2(range));
    }
    
    static RandomNumberGenerator global_;
};

template <class Engine>
RandomNumberGenerator<Engine> RandomNumberGenerator<Engine>::global_(RandomSeed);


template <class Engine>
double RandomNumberGenerator<Engine>::normal() const
{
    if(normalCachedValid_)
    {
        normalCachedValid_ = false;
        return normalCached_;
    }
    else
    {
        double x1, x2, w;
        do 
        {
             x1 = uniform(-1.0, 1.0);
             x2 = uniform(-1.0, 1.0);
             w = x1 * x1 + x2 * x2;
        } 
        while ( w > 1.0 || w == 0.0);
        
        w = std::sqrt( -2.0 * std::log( w )  / w );

        normalCached_ = x2 * w;
        normalCachedValid_ = true;

        return x1 * w;
    }
}

    /** Shorthand for the TT800 random number generator class.
    */
typedef RandomNumberGenerator<detail::RandomState<detail::TT800> >  RandomTT800; 

    /** Shorthand for the TT800 random number generator class (same as RandomTT800).
    */
typedef RandomNumberGenerator<detail::RandomState<detail::TT800> >  TemperedTwister; 

    /** Shorthand for the MT19937 random number generator class.
    */
typedef RandomNumberGenerator<detail::RandomState<detail::MT19937> > RandomMT19937;

    /** Shorthand for the MT19937 random number generator class (same as RandomMT19937).
    */
typedef RandomNumberGenerator<detail::RandomState<detail::MT19937> > MersenneTwister;

    /** Access the global (program-wide) instance of the TT800 random number generator.
    */
inline RandomTT800   & randomTT800()   { return RandomTT800::global(); }

    /** Access the global (program-wide) instance of the MT19937 random number generator.
    */
inline RandomMT19937 & randomMT19937() { return RandomMT19937::global(); }

template <class Engine>
class FunctorTraits<RandomNumberGenerator<Engine> >
{
  public:
    typedef RandomNumberGenerator<Engine> type;
    
    typedef VigraTrueType  isInitializer;
    
    typedef VigraFalseType isUnaryFunctor;
    typedef VigraFalseType isBinaryFunctor;
    typedef VigraFalseType isTernaryFunctor;
    
    typedef VigraFalseType isUnaryAnalyser;
    typedef VigraFalseType isBinaryAnalyser;
    typedef VigraFalseType isTernaryAnalyser;
};


/** Functor to create uniformly distributed integer random numbers.

    This functor encapsulates the appropriate functions of the given random number
    <tt>Engine</tt> (usually <tt>RandomTT800</tt> or <tt>RandomMT19937</tt>)
    in an STL-compatible interface. 
    
    <b>Traits defined:</b>
    
    \verbatim FunctorTraits<UniformIntRandomFunctor<Engine> >::isInitializer \endverbatim
    and
    \verbatim FunctorTraits<UniformIntRandomFunctor<Engine> >::isUnaryFunctor \endverbatim
    are true (<tt>VigraTrueType</tt>).
*/
template <class Engine = MersenneTwister>
class UniformIntRandomFunctor
{
    UInt32 lower_, difference_, factor_;
    Engine const & generator_;
    bool useLowBits_;

  public:
  
    typedef UInt32 argument_type; ///< STL required functor argument type
    typedef UInt32 result_type; ///< STL required functor result type

        /** Create functor for uniform random integers in the range [0, 2<sup>32</sup>) 
            using the given engine.
            
            That is, the generated numbers satisfy 0 &lt;= i &lt; 2<sup>32</sup>.
        */
    explicit UniformIntRandomFunctor(Engine const & generator = Engine::global() )
    : lower_(0), difference_(0xffffffff), factor_(1),
      generator_(generator),
      useLowBits_(true)
    {}
    
        /** Create functor for uniform random integers in the range [<tt>lower</tt>, <tt>upper</tt>]
            using the given engine.
            
            That is, the generated numbers satisfy <tt>lower</tt> &lt;= i &lt;= <tt>upper</tt>.
            \a useLowBits should be set to <tt>false</tt> when the engine generates
            random numbers whose low bits are significantly less random than the high
            bits. This does not apply to <tt>RandomTT800</tt> and <tt>RandomMT19937</tt>,
            but is necessary for simpler linear congruential generators.
        */
    UniformIntRandomFunctor(UInt32 lower, UInt32 upper, 
                            Engine const & generator = Engine::global(),
                            bool useLowBits = true)
    : lower_(lower), difference_(upper-lower), 
      factor_(Engine::factorForUniformInt(difference_ + 1)),
      generator_(generator),
      useLowBits_(useLowBits)
    {
        vigra_precondition(lower < upper,
          "UniformIntRandomFunctor(): lower bound must be smaller than upper bound."); 
    }
    
        /** Return a random number as specified in the constructor.
        */
    UInt32 operator()() const
    {
        if(difference_ == 0xffffffff) // lower_ is necessarily 0
            return generator_();
        else if(useLowBits_)
            return generator_.uniformInt(difference_+1) + lower_;
        else
        {
            UInt32 res = generator_() / factor_;

            // Use rejection method to avoid quantization bias.
            // On average, we will need two raw random numbers to generate one.
            while(res > difference_)
                res = generator_() / factor_;
            return res + lower_;
        }
    }

        /** Return a uniformly distributed integer random number in the range [0, <tt>beyond</tt>).
        
            That is, 0 &lt;= i &lt; <tt>beyond</tt>. This is a required interface for 
            <tt>std::random_shuffle</tt>. It ignores the limits specified 
            in the constructor and the flag <tt>useLowBits</tt>.
        */
    UInt32 operator()(UInt32 beyond) const
    {
        if(beyond < 2)
            return 0;

        return generator_.uniformInt(beyond);
    }
};

template <class Engine>
class FunctorTraits<UniformIntRandomFunctor<Engine> >
{
  public:
    typedef UniformIntRandomFunctor<Engine> type;
    
    typedef VigraTrueType  isInitializer;
    
    typedef VigraTrueType  isUnaryFunctor;
    typedef VigraFalseType isBinaryFunctor;
    typedef VigraFalseType isTernaryFunctor;
    
    typedef VigraFalseType isUnaryAnalyser;
    typedef VigraFalseType isBinaryAnalyser;
    typedef VigraFalseType isTernaryAnalyser;
};

/** Functor to create uniformly distributed double-precision random numbers.

    This functor encapsulates the function <tt>uniform()</tt> of the given random number
    <tt>Engine</tt> (usually <tt>RandomTT800</tt> or <tt>RandomMT19937</tt>)
    in an STL-compatible interface. 
    
    <b>Traits defined:</b>
    
    \verbatim FunctorTraits<UniformIntRandomFunctor<Engine> >::isInitializer \endverbatim
    is true (<tt>VigraTrueType</tt>).
*/
template <class Engine = MersenneTwister>
class UniformRandomFunctor
{
    double offset_, scale_;
    Engine const & generator_;

  public:
  
    typedef double result_type; ///< STL required functor result type

        /** Create functor for uniform random double-precision numbers in the range [0.0, 1.0] 
            using the given engine.
            
            That is, the generated numbers satisfy 0.0 &lt;= i &lt;= 1.0.
        */
    UniformRandomFunctor(Engine const & generator = Engine::global())
    : offset_(0.0),
      scale_(1.0),
      generator_(generator)
    {}

        /** Create functor for uniform random double-precision numbers in the range [<tt>lower</tt>, <tt>upper</tt>]
            using the given engine.
            
            That is, the generated numbers satisfy <tt>lower</tt> &lt;= i &lt;= <tt>upper</tt>.
        */
    UniformRandomFunctor(double lower, double upper, 
                         Engine & generator = Engine::global())
    : offset_(lower),
      scale_(upper - lower),
      generator_(generator)
    {
        vigra_precondition(lower < upper,
          "UniformRandomFunctor(): lower bound must be smaller than upper bound."); 
    }
    
        /** Return a random number as specified in the constructor.
        */
    double operator()() const
    {
        return generator_.uniform() * scale_ + offset_;
    }
};

template <class Engine>
class FunctorTraits<UniformRandomFunctor<Engine> >
{
  public:
    typedef UniformRandomFunctor<Engine> type;
    
    typedef VigraTrueType  isInitializer;
    
    typedef VigraFalseType isUnaryFunctor;
    typedef VigraFalseType isBinaryFunctor;
    typedef VigraFalseType isTernaryFunctor;
    
    typedef VigraFalseType isUnaryAnalyser;
    typedef VigraFalseType isBinaryAnalyser;
    typedef VigraFalseType isTernaryAnalyser;
};

/** Functor to create normal variate random numbers.

    This functor encapsulates the function <tt>normal()</tt> of the given random number
    <tt>Engine</tt> (usually <tt>RandomTT800</tt> or <tt>RandomMT19937</tt>)
    in an STL-compatible interface. 
    
    <b>Traits defined:</b>
    
    \verbatim FunctorTraits<UniformIntRandomFunctor<Engine> >::isInitializer \endverbatim
    is true (<tt>VigraTrueType</tt>).
*/
template <class Engine = MersenneTwister>
class NormalRandomFunctor
{
    double mean_, stddev_;
    Engine const & generator_;

  public:
  
    typedef double result_type; ///< STL required functor result type

        /** Create functor for standard normal random numbers 
            using the given engine.
            
            That is, mean is 0.0 and standard deviation is 1.0.
        */
    NormalRandomFunctor(Engine const & generator = Engine::global())
    : mean_(0.0),
      stddev_(1.0),
      generator_(generator)
    {}

        /** Create functor for normal random numbers with given mean and standard deviation
            using the given engine.
        */
    NormalRandomFunctor(double mean, double stddev, 
                        Engine & generator = Engine::global())
    : mean_(mean),
      stddev_(stddev),
      generator_(generator)
    {
        vigra_precondition(stddev > 0.0,
          "NormalRandomFunctor(): standard deviation must be positive."); 
    }
    
        /** Return a random number as specified in the constructor.
        */
    double operator()() const
    {
        return generator_.normal() * stddev_ + mean_;
    }

};

template <class Engine>
class FunctorTraits<NormalRandomFunctor<Engine> >
{
  public:
    typedef UniformRandomFunctor<Engine>  type;
    
    typedef VigraTrueType  isInitializer;
    
    typedef VigraFalseType isUnaryFunctor;
    typedef VigraFalseType isBinaryFunctor;
    typedef VigraFalseType isTernaryFunctor;
    
    typedef VigraFalseType isUnaryAnalyser;
    typedef VigraFalseType isBinaryAnalyser;
    typedef VigraFalseType isTernaryAnalyser;
};

//@}

} // namespace vigra 

#endif // VIGRA_RANDOM_HXX