CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
uniform_01.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_SPEED_UNIFORM_01_HPP
2 # define CPPAD_SPEED_UNIFORM_01_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 /*
15 $begin uniform_01$$
16 $spell
17  CppAD
18  namespace
19  cppad
20  hpp
21 $$
22 
23 $section Simulate a [0,1] Uniform Random Variate$$
24 $mindex uniform_01$$
25 
26 
27 $head Syntax$$
28 $codei%# include <cppad/speed/uniform_01.hpp>
29 %$$
30 $codei%uniform_01(%seed%)
31 %$$
32 $codei%uniform_01(%n%, %x%)%$$
33 
34 $head Purpose$$
35 This routine is used to create random values for speed testing purposes.
36 
37 $head Inclusion$$
38 The template function $code uniform_01$$ is defined in the $code CppAD$$
39 namespace by including
40 the file $code cppad/speed/uniform_01.hpp$$
41 (relative to the CppAD distribution directory).
42 
43 $head seed$$
44 The argument $icode seed$$ has prototype
45 $codei%
46  size_t %seed%
47 %$$
48 It specifies a seed
49 for the uniform random number generator.
50 
51 $head n$$
52 The argument $icode n$$ has prototype
53 $codei%
54  size_t %n%
55 %$$
56 It specifies the number of elements in the random vector $icode x$$.
57 
58 $head x$$
59 The argument $icode x$$ has prototype
60 $codei%
61  %Vector% &%x%
62 %$$.
63 The input value of the elements of $icode x$$ does not matter.
64 Upon return, the elements of $icode x$$ are set to values
65 randomly sampled over the interval [0,1].
66 
67 $head Vector$$
68 If $icode y$$ is a $code double$$ value,
69 the object $icode x$$ must support the syntax
70 $codei%
71  %x%[%i%] = %y%
72 %$$
73 where $icode i$$ has type $code size_t$$ with value less than
74 or equal $latex n-1$$.
75 This is the only requirement of the type $icode Vector$$.
76 
77 $children%
78  omh/uniform_01_hpp.omh
79 %$$
80 
81 $head Source Code$$
82 The file
83 $cref uniform_01.hpp$$
84 constraints the source code for this template function.
85 
86 $end
87 ------------------------------------------------------------------------------
88 */
89 // BEGIN C++
90 # include <cstdlib>
91 
92 namespace CppAD {
93  inline void uniform_01(size_t seed)
94  { std::srand( (unsigned int) seed); }
95 
96  template <class Vector>
97  void uniform_01(size_t n, Vector &x)
98  { static double factor = 1. / double(RAND_MAX);
99  while(n--)
100  x[n] = std::rand() * factor;
101  }
102 }
103 // END C++
104 # endif
void uniform_01(size_t seed)
Definition: uniform_01.hpp:93