From 98a01ea8f745a926d7f26f0706f5f95a401c5d83 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Thu, 29 Dec 2016 19:48:27 +0000 Subject: [PATCH] random.cpp: Added code and headers. Linux-only version so far. Gets 4 bits from the /dev/urandom pool. --- source/common/random.cpp | 47 ++++++++++++++++++++++++++++++++++++++++ source/common/random.h | 29 +++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 source/common/random.cpp create mode 100644 source/common/random.h diff --git a/source/common/random.cpp b/source/common/random.cpp new file mode 100644 index 0000000..c661320 --- /dev/null +++ b/source/common/random.cpp @@ -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 + +#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 index 0000000..212f942 --- /dev/null +++ b/source/common/random.h @@ -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 + +#ifndef __COMMON_RANDOM_H__ +#define __COMMON_RANDOM_H__ + +#include +#include +#include +#include + +int GenerateRandomNumber(int powerOf); + +#endif \ No newline at end of file -- 2.39.2