Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

smart_idiot

Random numbers that stay random.

This topic is 6038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

An interesting class I wrote because I was bored. Unfortunatly it will probably cause more harm than good in a real application.
  
/*

                           Random numbers that stay random.
                        Another useless creation by smart_idiot.

                  Uses the ''Mersenne Twister'' random number generator.
                      http://www.math.keio.ac.jp/matumoto/emt.html

  * Results are >= min and < max. You will never get the maximum!!! It doesn''t
    matter with floating point, but can be annoying with intigers and
    characters.

  * When you multiply random numbers by random numbers, although the range is
    correct, the probability for all parts of that range is equal, and it
    shouldn''t be.


*/

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

static unsigned long seed = 0;

template <class Type> class Number
 {
  public:
   Number() {min = max = 0.0;}
   Number(const Number &n) {min = n.min; max = n.max;}
   Number(const Type &n) {min = max = n;}
   Number(const Type &n, const Type &x) {min = n; max = x;}
   static Number Random(const Type &n, const Type &x) {Number r(n, x); return r;}

   operator Type()
    {
     unsigned long y;

     if (mti >= 624)
      {
       unsigned long mag01[2]={0x0, 0x9908b0df};
       int kk;

       if (mti == 625)
        {
         do
          {
           seed += time(0);
           seed ^= seed >> 11;
           seed ^= (seed << 7) & 0x9d2c5680;
           seed ^= (seed << 15) & 0xefc60000;
           seed ^= seed >> 18;
          }
         while(!seed);

         mt[0]= seed & 0xffffffff;
         for (mti=1; mti<624; mti++)
          mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
        }

       for (kk=0;kk<624-397;kk++)
        {
         y = (mt[kk]&0x80000000)|(mt[kk+1]&0x7fffffff);
         mt[kk] = mt[kk+397] ^ (y >> 1) ^ mag01[y & 0x1];
        }

       for (;kk<623;kk++)
        {
         y = (mt[kk]&0x80000000)|(mt[kk+1]&0x7fffffff);
         mt[kk] = mt[kk+(397-624)] ^ (y >> 1) ^ mag01[y & 0x1];
        }

       y = (mt[623]&0x80000000)|(mt[0]&0x7fffffff);
       mt[623] = mt[396] ^ (y >> 1) ^ mag01[y & 0x1];

       mti = 0;
      }
 
     y = mt[mti++];
     y ^= y >> 11;
     y ^= (y << 7) & 0x9d2c5680;
     y ^= (y << 15) & 0xefc60000;
     y ^= y >> 18;

     return (Type)((double)y/(double)0xffffffff*(max-min)+min);
    }

   Number operator +  (const Number &number) const {Number n(*this); n.min += number.min; n.max += number.max; return n;}
   Number operator +  (const Type &number) const {Number n(*this); n.min += number; n.max += number; return n;}
   Number operator -  (const Number &number) const {Number n(*this); n.min -= number.min; n.max -= number.max; return n;}
   Number operator -  (const Type &number) const {Number n(*this); n.min -= number; n.max -= number; return n;}
   Number operator *  (const Number &number) const {Number n(*this); n.min *= number.min; n.max *= number.max; return n;}
   Number operator *  (const Type &number) const {Number n(*this); n.min *= number; n.max *= number; return n;}
   Number operator /  (const Number &number) const {Number n(*this); n.min /= number.min; n.max /= number.max; return n;}
   Number operator /  (const Type &number) const {Number n(*this); n.min /= number; n.max /= number; return n;}
   Number operator =  (const Number &number) {min = number.min; max = number.max; return *this;}
   Number operator =  (const Type &number) {min = number; max = number; return *this;}
   Number operator += (const Number &number) {min += number.min; max += number.max; return *this;}
   Number operator += (const Type &number) {min += number; max += number; return *this;}
   Number operator -= (const Number &number) {min -= number.min; max -= number.max; return *this;}
   Number operator -= (const Type &number) {min -= number; max -= number; return *this;}
   Number operator *= (const Number &number) {min *= number.min; max *= number.max; return *this;}
   Number operator *= (const Type &number) {min *= number; max *= number; return *this;}
   Number operator /= (const Number &number) {min /= number.min; max /= number.max; return *this;}
   Number operator /= (const Type &number) {min /= number; max /= number; return *this;}
   bool operator == (const Number &number) const {return min==number.min && max==number.min;}
   bool operator == (const Type &number) const {return (min+max)/2.0 == number;}
   bool operator != (const Number &number) const {return min!=number.min || max!=number.min;}
   bool operator != (const Type &number) const {return (min+max)/2.0 != number;}
   bool operator >  (const Number &number) const {return min+max > number.min+number.max;}
   bool operator >  (const Type &number) const {return (min+max)/2.0 > number;}
   bool operator >= (const Number &number) const {return min+max >= number.min+number.max;}
   bool operator >= (const Type &number) const {return (min+max)/2.0 >= number;}
   bool operator <  (const Number &number) const {return min+max < number.min+number.max;}
   bool operator <  (const Type &number) const {return (min+max)/2.0 < number;}
   bool operator <= (const Number &number) const {return min+max <= number.min+number.max;}
   bool operator <= (const Type &number) const {return (min+max)/2.0 <= number;}

  private:
   Type min, max;
   static unsigned long mt[624];
   static int mti;
 };

template <class Type> unsigned long Number<Type>::mt[624];
template <class Type> int Number<Type>::mti = 625;

template <class Type> Number<Type> operator +  (const double &d, const Number<Type> &n) {return (Number<Type>)d + n;};
template <class Type> Number<Type> operator -  (const double &d, const Number<Type> &n) {return (Number<Type>)d - n;};
template <class Type> Number<Type> operator *  (const double &d, const Number<Type> &n) {return (Number<Type>)d * n;};
template <class Type> Number<Type> operator /  (const double &d, const Number<Type> &n) {return (Number<Type>)d / n;};
template <class Type> Number<Type> operator == (const double &d, const Number<Type> &n) {return (Number<Type>)d == n;};
template <class Type> Number<Type> operator != (const double &d, const Number<Type> &n) {return (Number<Type>)d != n;};
template <class Type> Number<Type> operator >  (const double &d, const Number<Type> &n) {return (Number<Type>)d > n;};
template <class Type> Number<Type> operator >= (const double &d, const Number<Type> &n) {return (Number<Type>)d >= n;};
template <class Type> Number<Type> operator <  (const double &d, const Number<Type> &n) {return (Number<Type>)d < n;};
template <class Type> Number<Type> operator <= (const double &d, const Number<Type> &n) {return (Number<Type>)d <= n;};


int main(int argc, char *argv[])
{
 cout << "Random characters: >= a && < z+1" << endl;
 cout << Number<char>::Random(''a'',''z''+1) << endl;
 cout << Number<char>::Random(''a'',''z''+1) << endl;
 cout << Number<char>::Random(''a'',''z''+1) << endl;
 cout << Number<char>::Random(''a'',''z''+1) << endl;

 cout << endl << "Random integers: >= 1 && < 10+1" << endl;
 cout << Number<int>::Random(1, 10+1) << endl;
 cout << Number<int>::Random(1, 10+1) << endl;
 cout << Number<int>::Random(1, 10+1) << endl;
 cout << Number<int>::Random(1, 10+1) << endl;

 cout << endl << "Random floating point numbers: >= 1.0 && < 10.0" << endl;
 cout << Number<double>::Random(1.0, 10.0) << endl;
 cout << Number<double>::Random(1.0, 10.0) << endl;
 cout << Number<double>::Random(1.0, 10.0) << endl;
 cout << Number<double>::Random(1.0, 10.0) << endl;

 cout << endl << "Math with random numbers: (n >= 0.0 && n < 1.0) * 2.0 - 2.0" << endl;
 Number<double> n(0.0, 2.0);
 n = n * 2.0 - 2.0;

 cout << n << endl;
 cout << n << endl;
 cout << n << endl;
 cout << n << endl;

 getc(stdin);
 return 0;
}
  

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!