Random.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2005-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #ifndef __RANDOM_H__
00018 #define __RANDOM_H__
00019 
00020 #include <stdlib.h>
00021 #include <vector>
00022 
00023 namespace oasys {
00024 
00028 class Random {
00029 public:
00033     static void seed(unsigned int seed) {
00034         srandom(seed);
00035         srand(seed);
00036     }
00037 
00051     static int rand(unsigned int max = RAND_MAX) {
00052         int ret = ::rand();
00053         if (max == RAND_MAX) {
00054             return ret;
00055         }
00056         return (ret % max);
00057     }
00058 };
00059 
00066 class ByteGenerator {
00067 public:
00068     ByteGenerator(unsigned int seed = 0);
00069 
00073     void fill_bytes(void* buf, size_t size);
00074 
00075     static const unsigned int A = 1277;
00076     static const unsigned int M = 131072;
00077     static const unsigned int C = 29574;
00078 
00079 private:
00080     unsigned int cur_;
00081     
00083     void next();
00084 };
00085 
00090 class PermutationArray {
00091 public:
00092     PermutationArray(size_t size);
00093     
00094     unsigned int map(unsigned int i);
00095 
00096 private:
00097     std::vector<unsigned int> array_;
00098     size_t size_;
00099 };
00100 
00101 }; // namespace oasys
00102 
00103 #endif //__RANDOM_H__

Generated on Thu Jun 7 16:56:51 2007 for DTN Reference Implementation by  doxygen 1.5.1