URandom.h

00001 #ifndef SCT_URANDOM_H
00002 #define SCT_URANDOM_H
00003 
00004 // Random numbers that are UNLIKELY to be the same every time!
00005 
00006 #include <fstream>
00007 
00008 namespace Sct {
00009 
00010   class URandom {
00011 
00012   public:
00013 
00014     static char urandchar() {
00015       static std::ifstream f("/dev/urandom");
00016       return f.get();
00017     };
00018     
00019     template <class T>
00020     static T urand() {
00021       T ans=0;
00022       for (unsigned int i=0; i<sizeof(T); i++) {
00023         ans<<=8;
00024         T k = ((static_cast<T>(urandchar()))&0xFF);
00025         //    std::cout << "k is " << k << " while ans is "  << ans << std::endl;
00026         ans|=k;
00027       };
00028       //std:: cout << "seed was " << ans << std::endl;
00029       return ans; 
00030     };
00031 
00032   };
00033 
00034 };
00035 
00036 #endif

Generated on Mon Feb 6 14:01:37 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6