00001 /* 00002 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva. 00003 * 00004 * This file may be distributed under the terms of the Q Public License 00005 * as defined by Trolltech AS of Norway and appearing in the file 00006 * LICENSE.QPL included in the packaging of this file. 00007 * 00008 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING 00009 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00010 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00011 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00012 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00013 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00014 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 * 00016 * Thanks: Jose Renato Santos 00017 * 00018 */ 00019 00020 /////////////////////////////////////////////////////////////////////////////// 00021 // Random.cpp: implementation of the Random class. 00022 /////////////////////////////////////////////////////////////////////////////// 00023 00024 #include "ObjectManager.h" 00025 #include "Random.h" 00026 00027 #include <stdlib.h> 00028 00029 #ifdef _DEBUG 00030 #undef THIS_FILE 00031 static char THIS_FILE[]=__FILE__; 00032 #define new DEBUG_NEW 00033 #endif 00034 00035 #include "time.h" 00036 00037 /////////////////////////////////////////////////////////////////////////////// 00038 void InitializeRandomNumber() 00039 { 00040 srand( time( NULL )); 00041 } 00042 00043 /////////////////////////////////////////////////////////////////////////////// 00044 unsigned int RandomNumber( const unsigned int range ) 00045 { 00046 unsigned int result; 00047 00048 // random number precision is 28 bits 00049 // return number out of range if range is larger than 28 bits 00050 if( range > 0xfffffff ) 00051 return 0xffffffff; 00052 result = ( rand() << 15 ) + rand(); 00053 result = result % range; 00054 return result; 00055 } 00056 00057 /////////////////////////////////////////////////////////////////////////////// 00058 double RandomProb() 00059 { 00060 double x = (( double ) RandomNumber( 0xfffffff ) ) / ( 0xfffffff ); 00061 return x; 00062 }