Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
random: Implemented Win32 version
[xestiacalendar/.git] / source / common / random.cpp
1 // random.cpp - Random number generation.
2 //
3 // (c) 2016-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Calendar is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Calendar is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "random.h"
21 using namespace std;
23 int GenerateRandomNumber(int powerOf){
24 #if defined(WIN32)
26         // Seed the randomiser!
28         srand(time(0));
30         int powerOfCalculation = (int)pow(2, (double)powerOf);
31         int randomNumber = rand() % powerOfCalculation;
33 #else 
34         // Get four bytes from /dev/urandom.
35         
36         int randomData = 0;
37         
38         ifstream randomFilehandle("/dev/urandom", ios::in|ios::binary);
39         char *randomDataInput;
40         
41         randomDataInput = new char [4];
42         randomFilehandle.read(randomDataInput, 4);
43         
44         // Seed the randomiser!
45         
46         srand(*randomDataInput);
47         delete [] randomDataInput;
48         
49         randomFilehandle.close();
50         
51         int powerOfCalculation = (int)pow(2, (double)powerOf);
52         int randomNumber = rand() % powerOfCalculation;
53 #endif
54         
55         return randomNumber;
56 }
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy