00001 # ifndef CPPAD_UNIFORM_01_INCLUDED 00002 # define CPPAD_UNIFORM_01_INCLUDED 00003 00004 /* -------------------------------------------------------------------------- 00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell 00006 00007 CppAD is distributed under multiple licenses. This distribution is under 00008 the terms of the 00009 Common Public License Version 1.0. 00010 00011 A copy of this license is included in the COPYING file of this distribution. 00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00013 -------------------------------------------------------------------------- */ 00014 /* 00015 $begin uniform_01$$ 00016 $spell 00017 CppAD 00018 cppad.hpp 00019 namespace 00020 $$ 00021 00022 $section Simulate a [0,1] Uniform Random Variate$$ 00023 00024 $index uniform_01$$ 00025 $index random, uniform vector$$ 00026 $index uniform, random vector$$ 00027 $index vector, uniform random$$ 00028 00029 $head Syntax$$ 00030 $syntax%# include <cppad/speed/uniform_01.hpp> 00031 %$$ 00032 $syntax%uniform_01(%seed%) 00033 %$$ 00034 $syntax%uniform_01(%n%, %x%)%$$ 00035 00036 $head Purpose$$ 00037 This routine is used to create random values for speed testing purposes. 00038 00039 $head Inclusion$$ 00040 The template function $code uniform_01$$ is defined in the $code CppAD$$ 00041 namespace by including 00042 the file $code cppad/speed/uniform_01.hpp$$ 00043 (relative to the CppAD distribution directory). 00044 It is only intended for example and testing purposes, 00045 so it is not automatically included by 00046 $cref/cppad.hpp/cppad/$$. 00047 00048 $head seed$$ 00049 The argument $italic seed$$ has prototype 00050 $syntax% 00051 size_t %seed% 00052 %$$ 00053 It specifies a seed 00054 for the uniform random number generator. 00055 00056 $head n$$ 00057 The argument $italic n$$ has prototype 00058 $syntax% 00059 size_t %n% 00060 %$$ 00061 It specifies the number of elements in the random vector $italic x$$. 00062 00063 $head x$$ 00064 The argument $italic x$$ has prototype 00065 $syntax% 00066 %Vector% &%x% 00067 %$$. 00068 The input value of the elements of $italic x$$ does not matter. 00069 Upon return, the elements of $italic x$$ are set to values 00070 randomly sampled over the interval [0,1]. 00071 00072 $head Vector$$ 00073 If $italic y$$ is a $code double$$ value, 00074 the object $italic x$$ must support the syntax 00075 $syntax% 00076 %x%[%i%] = %y% 00077 %$$ 00078 where $italic i$$ has type $code size_t$$ with value less than 00079 or equal $latex n-1$$. 00080 This is the only requirement of the type $italic Vector$$. 00081 00082 $children% 00083 omh/uniform_01_hpp.omh 00084 %$$ 00085 00086 $head Source Code$$ 00087 The file 00088 $cref/uniform_01.hpp/$$ 00089 constraints the source code for this template function. 00090 00091 $end 00092 ------------------------------------------------------------------------------ 00093 */ 00094 // BEGIN PROGRAM 00095 # include <cstdlib> 00096 00097 namespace CppAD { 00098 inline void uniform_01(size_t seed) 00099 { std::srand( (unsigned int) seed); } 00100 00101 template <class Vector> 00102 void uniform_01(size_t n, Vector &x) 00103 { static double factor = 1. / double(RAND_MAX); 00104 while(n--) 00105 x[n] = std::rand() * factor; 00106 } 00107 } 00108 // END PROGRAM 00109 # endif