Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Random.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "Random.h"
00010 
00011 #include <stdlib.h>
00012 #include <time.h>
00013 
00014 unsigned char Random::ms_randArray[];
00015 //----------------------------------------------------------
00016 /**
00017  *  Init random generator.
00018  */
00019 void
00020 Random::init() {
00021     srand( static_cast<unsigned>(time(NULL)) );
00022     for (int i = 0; i < ARRAY_SIZE; ++i) {
00023         ms_randArray[i] = randomInt(256);
00024     }
00025 }
00026 
00027 //-----------------------------------------------------------------
00028 /*
00029  * Return number from interval <0,bound).
00030  */
00031     int
00032 Random::randomInt(int bound)
00033 {
00034     if (bound == 0) {
00035         return 0;
00036     }
00037     return rand() % bound;
00038 }
00039 //-----------------------------------------------------------------
00040 /*
00041  * Return real number from interval <0,bound).
00042  */
00043     double
00044 Random::randomReal(double bound)
00045 {
00046     return bound * rand() / (RAND_MAX + 1.0);
00047 }
00048 //-----------------------------------------------------------------
00049 /**
00050  * Return a value from interval <0,255>.
00051  * @param index index of byte, the byte and a index is alway the same
00052  */
00053     unsigned char
00054 Random::aByte(unsigned int index)
00055 {
00056     return ms_randArray[index % ARRAY_SIZE];
00057 }
00058 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2