Page principale Liste des namespaces Hiérarchie des classes Liste par ordre alphabétique Liste des composants Liste des fichiers Membres des namespaces Composants Déclarations

# ExCRandomNumber.cpp

Aller à la documentation de ce fichier.

    00001 /*
    00002  * ExNihilo 3D Engine
    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  *  This program is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  *  GNU Library General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU General Public License
    00015  *  along with this program; if not, write to the Free Software
    00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00017  *
    00018  * Please read AUTHORS file !!!
    00019  *
    00020  * $Id: ExCRandomNumber.cpp,v 1.11 2002/06/21 19:04:10 binny Exp $
    00021  *
    00022  */
    00023
    00024 #include "ExCRandomNumber.h"
    00025
    00026 int ExCRandomNumber::is_init = false;
    00027
    00028 ExCRandomNumber::ExCRandomNumber () {
    00029         if (! is_init) {
    00030                 srand (time (NULL));
    00031                 is_init = true;
    00032         }
    00033 }
    00034
    00035 uint ExCRandomNumber::getInt (uint min, uint max) {
    00036         uint value;
    00037         uint v_tmp;
    00038         if (max < min) {
    00039                 v_tmp = min;
    00040                 min = max;
    00041                 max = v_tmp;
    00042         }
    00043
    00044         do {
    00045                 value = (rand() % (max - min + 1)) + min;
    00046         } while (value < min || value > max);
    00047
    00048         return value;
    00049 }
    00050
    00051 float ExCRandomNumber::getFloat (float min, float max) {
    00052         float value;
    00053         float v_tmp;
    00054         if (max < min) {
    00055                 v_tmp = min;
    00056                 min = max;
    00057                 max = v_tmp;
    00058         }
    00059
    00060         do {
    00061                 value = min + (float) getInt(0, (unsigned) ((max - min) * 1000)) / 1000;
    00062         } while (value < min || value > max);
    00063
    00064         return value;
    00065 }