Changed the rand object so you don't have to say rand::kernel_1a or

rand::float_1a anymore to declare it.  Now you just say rand.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404249
This commit is contained in:
Davis King 2011-05-04 21:09:22 +00:00
parent f780abea15
commit 98045930fe
5 changed files with 95 additions and 216 deletions

View File

@ -4,37 +4,6 @@
#define DLIB_RANd_
#include "rand/rand_kernel_1.h"
#include "rand/rand_float_1.h"
#include "algs.h"
namespace dlib
{
class rand
{
rand() {}
public:
//----------- kernels ---------------
// kernel_1a
typedef rand_kernel_1
kernel_1a;
//---------- extensions ------------
// float_1 extend kernel_1a
typedef rand_float_1<kernel_1a>
float_1a;
};
}
#endif // DLIB_RANd_

View File

@ -1,106 +0,0 @@
// Copyright (C) 2007 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_RAND_FLOAt_1_
#define DLIB_RAND_FLOAt_1_
#include "rand_float_abstract.h"
#include "../algs.h"
#include <limits>
#include "../uintn.h"
namespace dlib
{
template <
typename rand_base
>
class rand_float_1 : public rand_base
{
double max_val;
public:
rand_float_1 ()
{
max_val = 0xFFFFFF;
max_val *= 0x1000000;
max_val += 0xFFFFFF;
max_val += 0.01;
}
double get_random_double (
)
{
uint32 temp;
temp = rand_base::get_random_32bit_number();
temp &= 0xFFFFFF;
double val = static_cast<double>(temp);
val *= 0x1000000;
temp = rand_base::get_random_32bit_number();
temp &= 0xFFFFFF;
val += temp;
val /= max_val;
if (val < 1.0)
{
return val;
}
else
{
// return a value slightly less than 1.0
return 1.0 - std::numeric_limits<double>::epsilon();
}
}
float get_random_float (
)
{
uint32 temp;
temp = rand_base::get_random_32bit_number();
temp &= 0xFFFFFF;
const float scale = 1.0/0x1000000;
const float val = static_cast<float>(temp)*scale;
if (val < 1.0f)
{
return val;
}
else
{
// return a value slightly less than 1.0
return 1.0f - std::numeric_limits<float>::epsilon();
}
}
};
template <
typename rand_base
>
inline void swap (
rand_float_1<rand_base>& a,
rand_float_1<rand_base>& b
) { a.swap(b); }
// ----------------------------------------------------------------------------------------
template <typename rand_base>
struct is_rand<rand_float_1<rand_base> >
{
static const bool value = true;
};
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_RAND_FLOAt_1_

View File

@ -1,64 +0,0 @@
// Copyright (C) 2007 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_RAND_FLOAt_ABSTRACT_
#ifdef DLIB_RAND_FLOAt_ABSTRACT_
#include "rand_kernel_abstract.h"
namespace dlib
{
template <
typename rand_base
>
class rand_float : public rand_base
{
/*!
REQUIREMENTS ON RAND_BASE
RAND_BASE is instantiated with type T and
is an implementation of rand/rand_kernel_abstract.h
WHAT THIS EXTENSION DOES FOR RAND
This gives rand the ability to generate random float values.
!*/
public:
float get_random_float (
);
/*!
ensures
- returns a random float number N where: 0.0 <= N < 1.0.
throws
- std::bad_alloc
!*/
double get_random_double (
);
/*!
ensures
- returns a random double number N where: 0.0 <= N < 1.0.
throws
- std::bad_alloc
!*/
};
template <
template rand_base
>
inline void swap (
rand_float<rand_base>& a,
rand_float<rand_base>& b
) { a.swap(b); }
/*!
provides a global swap function
!*/
}
#endif // DLIB_RAND_FLOAt_ABSTRACT_

View File

@ -15,7 +15,7 @@ namespace dlib
{
class rand_kernel_1
class rand
{
/*!
@ -29,15 +29,24 @@ namespace dlib
public:
rand_kernel_1(
// These typedefs are here for backwards compatibility with older versions of dlib.
typedef rand kernel_1a;
typedef rand float_1a;
rand(
)
{
// prime the generator a bit
for (int i = 0; i < 10000; ++i)
mt();
max_val = 0xFFFFFF;
max_val *= 0x1000000;
max_val += 0xFFFFFF;
max_val += 0.01;
}
virtual ~rand_kernel_1(
virtual ~rand(
)
{}
@ -103,8 +112,61 @@ namespace dlib
return mt();
}
double get_random_double (
)
{
uint32 temp;
temp = rand::get_random_32bit_number();
temp &= 0xFFFFFF;
double val = static_cast<double>(temp);
val *= 0x1000000;
temp = rand::get_random_32bit_number();
temp &= 0xFFFFFF;
val += temp;
val /= max_val;
if (val < 1.0)
{
return val;
}
else
{
// return a value slightly less than 1.0
return 1.0 - std::numeric_limits<double>::epsilon();
}
}
float get_random_float (
)
{
uint32 temp;
temp = rand::get_random_32bit_number();
temp &= 0xFFFFFF;
const float scale = 1.0/0x1000000;
const float val = static_cast<float>(temp)*scale;
if (val < 1.0f)
{
return val;
}
else
{
// return a value slightly less than 1.0
return 1.0f - std::numeric_limits<float>::epsilon();
}
}
void swap (
rand_kernel_1& item
rand& item
)
{
exchange(mt,item.mt);
@ -112,12 +174,12 @@ namespace dlib
}
friend void serialize(
const rand_kernel_1& item,
const rand& item,
std::ostream& out
);
friend void deserialize(
rand_kernel_1& item,
rand& item,
std::istream& in
);
@ -125,23 +187,26 @@ namespace dlib
mt19937 mt;
std::string seed;
double max_val;
};
inline void swap (
rand_kernel_1& a,
rand_kernel_1& b
rand& a,
rand& b
) { a.swap(b); }
template <>
struct is_rand<rand_kernel_1>
struct is_rand<rand>
{
static const bool value = true;
};
inline void serialize(
const rand_kernel_1& item,
const rand& item,
std::ostream& out
)
{
@ -150,7 +215,7 @@ namespace dlib
}
inline void deserialize(
rand_kernel_1& item,
rand& item,
std::istream& in
)
{

View File

@ -19,10 +19,7 @@ namespace dlib
WHAT THIS OBJECT REPRESENTS
this object represents a pseudorandom number generator.
note that different implementations do not necessairly return the
same sequence of random numbers given the same seed.
This object represents a pseudorandom number generator.
!*/
public:
@ -97,6 +94,24 @@ namespace dlib
- std::bad_alloc
!*/
float get_random_float (
);
/*!
ensures
- returns a random float number N where: 0.0 <= N < 1.0.
throws
- std::bad_alloc
!*/
double get_random_double (
);
/*!
ensures
- returns a random double number N where: 0.0 <= N < 1.0.
throws
- std::bad_alloc
!*/
void swap (
rand& item
);