Boost logo

Boost :

From: James Curran/MVP (JamesCurran_at_[hidden])
Date: 2000-02-24 00:26:49


I'm been meaning to send this in for a few months now, and since the general
subject has turned to it, now seems like a good time. It's my first stab at
a RandomIter class. It used srand/rand (gag), but I wrote it well over a
year ago, before I saw the boost random number generators. It doesn't
implement any form of past-the-end (I thought I had written one).
(apologies for the obviously VC-wizard generated code)
// Random.h: interface for the Random class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_RANDOM_H__5CC82953_3DF1_11D2_9968_00AA0020E483__INCLUDED_)
#define AFX_RANDOM_H__5CC82953_3DF1_11D2_9968_00AA0020E483__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <iterator>
#include <stdlib.h>
#include <time.h>

class Random : public std::iterator<std::forward_iterator_tag, int>
{
public:
        explicit Random(int U): LowerBound(0), UpperBound(U)
                {srand( (unsigned)time( NULL ) ); generate();}
        Random(int L, int U) : LowerBound(L), UpperBound(U)
                {srand( (unsigned)time( NULL ) ); generate();}
        ~Random() {} ;

        void operator=(unsigned int seed) { srand(seed); }
        int operator *() const {return Current; }

        Random& operator ++() { generate(); return(*this); }
        Random operator ++(int) { generate(); return(*this); }

        friend bool operator==(const Random& lhs, const Random& rhs);

private:
        int LowerBound;
        int UpperBound;
        int Current;
        void generate()
        { Current = (((long)rand() * (UpperBound-LowerBound+1))/ RAND_MAX) +
LowerBound;}
};

#endif //
!defined(AFX_RANDOM_H__5CC82953_3DF1_11D2_9968_00AA0020E483__INCLUDED_)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk