| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "Random.h" |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <time.h> |
| 13 |
|
| 14 |
unsigned char Random::ms_randArray[]; |
| 15 |
//---------------------------------------------------------- |
| 16 |
/** |
| 17 |
* Init random generator. |
| 18 |
*/ |
| 19 |
void |
| 20 |
Random::init() { |
| 21 |
srand( static_cast<unsigned>(time(NULL)) ); |
| 22 |
for (int i = 0; i < ARRAY_SIZE; ++i) { |
| 23 |
ms_randArray[i] = randomInt(256); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
//----------------------------------------------------------------- |
| 28 |
/* |
| 29 |
* Return number from interval <0,bound). |
| 30 |
*/ |
| 31 |
int |
| 32 |
Random::randomInt(int bound) |
| 33 |
{ |
| 34 |
if (bound == 0) { |
| 35 |
return 0; |
| 36 |
} |
| 37 |
return rand() % bound; |
| 38 |
} |
| 39 |
//----------------------------------------------------------------- |
| 40 |
/* |
| 41 |
* Return real number from interval <0,bound). |
| 42 |
*/ |
| 43 |
double |
| 44 |
Random::randomReal(double bound) |
| 45 |
{ |
| 46 |
return bound * rand() / (RAND_MAX + 1.0); |
| 47 |
} |
| 48 |
//----------------------------------------------------------------- |
| 49 |
/** |
| 50 |
* Return a value from interval <0,255>. |
| 51 |
* @param index index of byte, the byte at a index is alway the same |
| 52 |
*/ |
| 53 |
unsigned char |
| 54 |
Random::aByte(unsigned int index) |
| 55 |
{ |
| 56 |
return ms_randArray[index % ARRAY_SIZE]; |
| 57 |
} |
| 58 |
|