From 25056b3623554fcdb48fcb387edf55c19ada3848 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 29 Jan 2011 14:23:07 +0000 Subject: [PATCH] added function for generating random ascii strings, and improved the test program to test for this as well git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3992 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/tests/general/random_test.cc | 1 + libretroshare/src/util/rsrandom.cc | 16 +++++++++++++++- libretroshare/src/util/rsrandom.h | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/libretroshare/src/tests/general/random_test.cc b/libretroshare/src/tests/general/random_test.cc index 3edfb6e98..6232829ab 100644 --- a/libretroshare/src/tests/general/random_test.cc +++ b/libretroshare/src/tests/general/random_test.cc @@ -132,6 +132,7 @@ int main(int argc, char **argv) feenableexcept(FE_INVALID) ; feenableexcept(FE_DIVBYZERO) ; #endif + std::cerr << "Generating random 64 chars string (run that again to test that it's changing): " << RSRandom::random_alphaNumericString(64) << std::endl; int nt = 10 ; // number of threads. std::vector threads(nt,(myThread*)NULL) ; diff --git a/libretroshare/src/util/rsrandom.cc b/libretroshare/src/util/rsrandom.cc index b05a4e252..c1d346be9 100644 --- a/libretroshare/src/util/rsrandom.cc +++ b/libretroshare/src/util/rsrandom.cc @@ -1,10 +1,14 @@ #include +#include +#include #include "rsrandom.h" uint32_t RSRandom::index = 0 ; std::vector RSRandom::MT(RSRandom::N,0u) ; RsMutex RSRandom::rndMtx ; -static bool auto_seed = RSRandom::seed(time(NULL)) ; + +// If this does not compile on windows, use a ifdef to keep this on linux plz. +static bool auto_seed = RSRandom::seed( (time(NULL) + pthread_self()*0x1293fe + (getpid()^0x113ef76b))^0x18e34a12 ) ; bool RSRandom::seed(uint32_t s) { @@ -71,3 +75,13 @@ double RSRandom::random_f64() return random_u64() / (double)(~(uint64_t)0) ; } +std::string RSRandom::random_alphaNumericString(uint32_t len) +{ + std::string s = "" ; + + for(int i=0;i