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
This commit is contained in:
csoler 2011-01-29 14:23:07 +00:00
parent 13132fe248
commit 25056b3623
3 changed files with 18 additions and 1 deletions

View File

@ -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<myThread *> threads(nt,(myThread*)NULL) ;

View File

@ -1,10 +1,14 @@
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include "rsrandom.h"
uint32_t RSRandom::index = 0 ;
std::vector<uint32_t> 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<len;++i)
s += (char)( (random_u32()%94) + 33) ;
return s ;
}

View File

@ -44,6 +44,8 @@ class RSRandom
static bool seed(uint32_t s) ;
static std::string random_alphaNumericString(uint32_t length) ;
private:
static RsMutex rndMtx ;