00001 /* 00002 * File: Random.cpp 00003 * Project: DUtils library 00004 * Author: Dorian Galvez-Lopez 00005 * Date: April 2010 00006 * Description: manages pseudo-random numbers 00007 * 00008 * 00009 * This program is free software: you can redistribute it and/or modify 00010 * it under the terms of the GNU Lesser General Public License as published by 00011 * the Free Software Foundation, either version 3 of the License, or 00012 * any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public License 00020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 */ 00023 00024 #include "Random.h" 00025 #include "Timestamp.h" 00026 #include <cstdlib> 00027 using namespace std; 00028 00029 bool DUtils::Random::m_seeded_current_time = false; 00030 bool DUtils::Random::m_seeded_int = false; 00031 00032 void DUtils::Random::SeedRand(){ 00033 Timestamp time; 00034 time.setToCurrentTime(); 00035 srand((unsigned)time.getFloatTime()); 00036 } 00037 00038 void DUtils::Random::SeedRandOnce() 00039 { 00040 if(!m_seeded_current_time) 00041 { 00042 DUtils::Random::SeedRand(); 00043 m_seeded_current_time = true; 00044 } 00045 } 00046 00047 void DUtils::Random::SeedRand(int seed) 00048 { 00049 srand(seed); 00050 } 00051 00052 void DUtils::Random::SeedRandOnce(int seed) 00053 { 00054 if(!m_seeded_int) 00055 { 00056 DUtils::Random::SeedRand(seed); 00057 m_seeded_int = true; 00058 } 00059 } 00060 00061 int DUtils::Random::RandomInt(int min, int max){ 00062 int d = max - min + 1; 00063 return int(((double)rand()/((double)RAND_MAX + 1.0)) * d) + min; 00064 } 00065 00066