[go: up one dir, main page]

Menu

[5a71d8]: / common / rand.h  Maximize  Restore  History

Download this file

70 lines (58 with data), 1.4 kB

 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
#ifndef STXXL_RAND_HEADER
#define STXXL_RAND_HEADER
/***************************************************************************
* rand.h
*
* Sat Aug 24 23:53:36 2002
* Copyright 2002 Roman Dementiev
* dementiev@mpi-sb.mpg.de
****************************************************************************/
#include <time.h>
#include <stdlib.h>
namespace stxxl
{
unsigned ran32State = time (NULL);
struct random_number32
{
inline unsigned operator () () const
{
return (ran32State = 1664525 * ran32State + 1013904223);
};
};
//! \brief Fast uniform [0,1] pseudo-random generator
struct random_uniform_fast
{
random_number32 rnd32;
inline double operator() () const
{
return ((double)rnd32() * (0.5 / 0x80000000));
}
};
//! \brief Slow and precise uniform [0,1] pseudo-random generator
//! \warning Seed is not the same as in the fast generator \c random_uniform_fast
struct random_uniform_slow
{
inline double operator() () const
{
return drand48();
}
};
template <class UniformRGen_ = random_uniform_fast>
struct random_number
{
UniformRGen_ uniform;
inline unsigned operator () (int N) const
{
return ((unsigned)(uniform() * (N)));
};
};
struct random_number64
{
random_uniform_slow uniform;
inline unsigned long long operator() () const
{
return static_cast<unsigned long long>(uniform()*(18446744073709551616.));
}
};
}
#endif