## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csutil/floatrand.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2004 by Jorrit Tyberghein
    00003     Copyright (C) 2005 by Eric Sunshine
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CS_CSUTIL_FLOATRAND_H__
    00021 #define __CS_CSUTIL_FLOATRAND_H__
    00022
    00027 #include "csextern.h"
    00028
    00032 class CS_CRYSTALSPACE_EXPORT csRandomFloatGen
    00033 {
    00034 private:
    00035   unsigned int seed;
    00036
    00037 public:
    00039   csRandomFloatGen ()
    00040   { Initialize(); }
    00042   csRandomFloatGen (unsigned int seed)
    00043   { Initialize(seed); }
    00044
    00046   void Initialize();
    00048   void Initialize(unsigned int new_seed)
    00049   { seed = new_seed; }
    00050
    00052   inline float Get()
    00053   {
    00054     unsigned int const b = 1664525;
    00055     unsigned int const c = 1013904223;
    00056     seed = b * seed + c;
    00057     uint32 temp = 0x3f800000 | (0x007fffff & seed);
    00058     return (*(float*)&temp) - 1.0;
    00059   }
    00060
    00062   inline float Get(float max)
    00063   {
    00064     return max * Get();
    00065   }
    00066
    00068   inline float Get(float min, float max)
    00069   {
    00070     float const w = max - min;
    00071     return min + w * Get();
    00072   }
    00073
    00075   inline float GetAngle()
    00076   {
    00077     return TWO_PI * Get();
    00078   }
    00079 };
    00080
    00081 #endif // __CS_CSUTIL_FLOATRAND_H__