random.cpp
1 //------------------------------------------------------------------------------
2 // random.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "random.h"
24 
25 #include <cmath>
26 #include <cstdlib>
27 #include <ctime>
28 
29 #include <QDateTime>
30 
31 bool Random::bIsInit = false;
32 
33 void Random::builtInInit()
34 {
35  if (!bIsInit)
36  {
37  QDateTime currentTime = QDateTime::currentDateTime();
38  init(clock() + currentTime.toTime_t());
39  }
40 }
41 
42 void Random::init(int seed)
43 {
44  bIsInit = true;
45  srand(seed);
46 }
47 
48 unsigned short Random::nextUShort(unsigned short max)
49 {
50  builtInInit();
51 
52  return rand() % max;
53 }
static unsigned short nextUShort(unsigned short max)
Generates a new random unsigned short.
Definition: random.cpp:48
static void init(int seed)
Inits or re-inits the Random class with specified seed.
Definition: random.cpp:42