Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
random.cpp: Added code and headers.
authorSteve Brokenshire <sbrokenshire@xestia.co.uk>
Thu, 29 Dec 2016 19:48:27 +0000 (19:48 +0000)
committerSteve Brokenshire <sbrokenshire@xestia.co.uk>
Thu, 29 Dec 2016 19:48:27 +0000 (19:48 +0000)
Linux-only version so far. Gets 4 bits from the /dev/urandom pool.

source/common/random.cpp [new file with mode: 0644]
source/common/random.h [new file with mode: 0644]

diff --git a/source/common/random.cpp b/source/common/random.cpp
new file mode 100644 (file)
index 0000000..c661320
--- /dev/null
@@ -0,0 +1,47 @@
+// random.cpp - Random number generation.
+//
+// (c) 2016 Xestia Software Development.
+//
+// This file is part of Xestia Calendar.
+//
+// Xestia Calendar is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by the
+// Free Software Foundation, version 3 of the license.
+//
+// Xestia Calendar is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
+
+#include "random.h"
+
+using namespace std;
+
+int GenerateRandomNumber(int powerOf){
+       
+       // Get four bytes from /dev/urandom.
+       
+       int randomData = 0;
+       
+       ifstream randomFilehandle("/dev/urandom", ios::in|ios::binary);
+       char *randomDataInput;
+       
+       randomDataInput = new char [4];
+       randomFilehandle.read(randomDataInput, 4);
+       
+       // Seed the randomiser!
+       
+       srand(*randomDataInput);
+       delete [] randomDataInput;
+       
+       randomFilehandle.close();
+       
+       int powerOfCalculation = (int)pow(2, (double)powerOf);
+       int randomNumber = rand() % powerOfCalculation;
+       
+       return randomNumber;
+       
+}
\ No newline at end of file
diff --git a/source/common/random.h b/source/common/random.h
new file mode 100644 (file)
index 0000000..212f942
--- /dev/null
@@ -0,0 +1,29 @@
+// random.h - Random number generation.
+//
+// (c) 2016 Xestia Software Development.
+//
+// This file is part of Xestia Calendar.
+//
+// Xestia Calendar is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by the
+// Free Software Foundation, version 3 of the license.
+//
+// Xestia Calendar is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
+
+#ifndef __COMMON_RANDOM_H__
+#define __COMMON_RANDOM_H__
+
+#include <cstdlib>
+#include <cmath>
+#include <iostream>
+#include <fstream>
+
+int GenerateRandomNumber(int powerOf);
+
+#endif
\ No newline at end of file
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