From ce793e31f387c0715fa5b50c20e06510cc3e95ff Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 22 May 2012 17:15:40 +0000 Subject: Moved random to stable, just needs some minor tweaks. But it's already in use in a couple of core components, including in tempFile name generation. --- src/stable/randomsystem.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/stable/randomsystem.cpp (limited to 'src/stable/randomsystem.cpp') diff --git a/src/stable/randomsystem.cpp b/src/stable/randomsystem.cpp new file mode 100644 index 0000000..0501587 --- /dev/null +++ b/src/stable/randomsystem.cpp @@ -0,0 +1,40 @@ +#include "bu/randomsystem.h" +#include "bu/file.h" + +Bu::RandomSystem::RandomSystem( Type eType ) : + eType( eType ), + pSrc( 0 ) +{ + switch( eType ) + { + case Bu::RandomSystem::Fast: + pSrc = new Bu::File("/dev/urandom", Bu::File::Read ); + break; + + case Bu::RandomSystem::Good: + pSrc = new Bu::File("/dev/random", Bu::File::Read ); + break; + } +} + +Bu::RandomSystem::~RandomSystem() +{ + delete pSrc; +} + +void Bu::RandomSystem::seed( int32_t /*iSeed*/ ) +{ + // Seed really has no effect here... + // on linux, if we were root, we could write data to random/urandom to + // perturb the data, but it's not necesarry +} + +int32_t Bu::RandomSystem::rand() +{ + if( !pSrc ) + throw Bu::ExceptionBase("Not initialized"); + int32_t i; + pSrc->read( &i, sizeof(int32_t) ); + return i; +} + -- cgit v1.2.3