SuperNN  1.0.0
utils.cpp
1 /*
2  This file is part of SuperNN.
3 
4  SuperNN is free software: you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  SuperNN is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with SuperNN. If not, see <http://www.gnu.org/licenses/>.
16 
17  Copyright (C) 2010 - 2015 Lucas Hermann Negri
18 */
19 
20 #include <cstdlib>
21 #include <cstdio>
22 
23 #ifndef _WIN32
24 #include <sys/time.h>
25 #else
26 #include <ctime>
27 #endif
28 #include "utils.hpp"
29 
30 namespace SuperNN
31 {
32 
33 namespace Utils
34 {
35 
36 double rand_double(double max)
37 {
38  return rand() / (RAND_MAX / max);
39 }
40 
41 void rand_seed()
42 {
43  unsigned seed;
44 
45 #ifndef _WIN32
46  FILE *rf = fopen("/dev/urandom", "r");
47 
48  if(!rf)
49  {
50  struct timeval tv;
51  gettimeofday(&tv, NULL);
52  seed = tv.tv_usec + tv.tv_sec;
53  }
54  else
55  {
56  fread(&seed, sizeof(seed), 1, rf);
57  fclose(rf);
58  }
59 
60 #else
61  seed = (unsigned)time(NULL);
62 #endif
63  srand(seed);
64 }
65 
66 }
67 }
SUPERNN_EXPORT void rand_seed()
Initializes the random number generator.
Definition: utils.cpp:41
SUPERNN_EXPORT double rand_double(double max)
Returns a pseudo-random double.
Definition: utils.cpp:36