00001 ////////////////////////////////////////////////////////////////////////////////////////////////// 00002 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. 00003 // Produced at the Lawrence Livermore National Laboratory 00004 // LLNL-CODE-433662 00005 // All rights reserved. 00006 // 00007 // This file is part of Muster. For details, see http://github.com/tgamblin/muster. 00008 // Please also read the LICENSE file for further information. 00009 // 00010 // Redistribution and use in source and binary forms, with or without modification, are 00011 // permitted provided that the following conditions are met: 00012 // 00013 // * Redistributions of source code must retain the above copyright notice, this list of 00014 // conditions and the disclaimer below. 00015 // * Redistributions in binary form must reproduce the above copyright notice, this list of 00016 // conditions and the disclaimer (as noted below) in the documentation and/or other materials 00017 // provided with the distribution. 00018 // * Neither the name of the LLNS/LLNL nor the names of its contributors may be used to endorse 00019 // or promote products derived from this software without specific prior written permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 00022 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00023 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 00024 // LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE 00025 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00026 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00027 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00028 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00029 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 ////////////////////////////////////////////////////////////////////////////////////////////////// 00031 00032 /// 00033 /// @file random.h 00034 /// @author Todd Gamblin tgamblin@llnl.gov 00035 /// @brief Helper functions for taking random samples and seeding 00036 /// RNGs from the system clock. 00037 /// 00038 #ifndef MUSTER_RANDOM_H 00039 #define MUSTER_RANDOM_H 00040 00041 #include <sys/time.h> 00042 00043 namespace cluster { 00044 00045 /// 00046 /// This is Knuth's algorithm R for taking a sample of indices from 00047 /// 0 to numElements. We sample size indices from this (the superset) 00048 /// and put them in the subset's mapping. 00049 /// 00050 /// @param numElements total number of elements to select from 00051 /// @param sample_size number of elements to select 00052 /// @param out destination for selected elements 00053 /// @param random model of STL Random Number Generator. 00054 /// must be callable as random(N), returning a random number in [0,N). 00055 /// 00056 template <class OutputIterator, class Random> 00057 void random_subset(size_t numElements, size_t sample_size, OutputIterator out, Random& random) { 00058 size_t first = 0; 00059 size_t remaining = numElements; 00060 size_t m = sample_size; 00061 00062 while (m > 0) { 00063 if ((size_t)random(remaining) < m) { 00064 *out = first; 00065 ++out; 00066 --m; 00067 } 00068 --remaining; 00069 ++first; 00070 } 00071 } 00072 00073 00074 /// 00075 /// Returns a reasonably distributed seed for random number generators. 00076 /// Based on the product of the seconds and usec in gettimeofday(). 00077 /// 00078 inline long get_time_seed() { 00079 struct timeval time; 00080 gettimeofday(&time, 0); 00081 return time.tv_sec * time.tv_usec; 00082 } 00083 00084 } // namespace cluster 00085 00086 #endif // MUSTER_RANDOM_H