From c838ce513d64b4c4233115ef610ccb1e43d3564f Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Fri, 25 Aug 2006 15:15:13 +0000 Subject: [PATCH] initialize random seed; use high-order bits for better randomness --- tests/progs/slapd-bind.c | 15 +++++++++------ tests/progs/slapd-read.c | 9 ++++++++- tests/progs/slapd-search.c | 8 +++++++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/tests/progs/slapd-bind.c b/tests/progs/slapd-bind.c index 4a5f09d541..11db20b1b5 100644 --- a/tests/progs/slapd-bind.c +++ b/tests/progs/slapd-bind.c @@ -288,7 +288,7 @@ do_base( char *uri, char *dn, struct berval *pass, char *base, char *filter, cha int version = LDAP_VERSION3; char *nullstr = ""; - srand(pid); + srand( pid ); ldap_initialize( &ld, uri ); if ( ld == NULL ) { @@ -397,16 +397,19 @@ novals:; /* Ok, got list of DNs, now start binding to each */ for ( i = 0; i < maxloop; i++ ) { - int j, k; - struct berval cred = { 0, NULL }; + int j; + struct berval cred = { 0, NULL }; - for ( j = 0, k = 0; k < ndns; k++) { - j = rand() % ndns; - } + +#if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */ + j = rand() % ndns; +#endif + j = ((double)ndns)*rand()/(RAND_MAX + 1.0); if ( creds && !BER_BVISEMPTY( &creds[j] ) ) { cred = creds[j]; } + if ( do_bind( uri, dns[j], &cred, 1, force, chaserefs, noinit, &ld ) && !force ) { diff --git a/tests/progs/slapd-read.c b/tests/progs/slapd-read.c index 0de1b109f6..0bc00d20a9 100644 --- a/tests/progs/slapd-read.c +++ b/tests/progs/slapd-read.c @@ -215,6 +215,8 @@ do_random( char *uri, char *manager, struct berval *passwd, char **values = NULL; LDAPMessage *res = NULL, *e = NULL; + srand( pid ); + attrs[ 0 ] = LDAP_NO_ATTRS; attrs[ 1 ] = NULL; @@ -275,7 +277,12 @@ do_random( char *uri, char *manager, struct berval *passwd, } for ( i = 0; i < innerloop; i++ ) { - do_read( uri, manager, passwd, values[ rand() % nvalues ], &ld, +#if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */ + int r = rand() % nvalues; +#endif + int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0); + + do_read( uri, manager, passwd, values[ r ], &ld, noattrs, 1, maxretries, delay, force, chaserefs ); } diff --git a/tests/progs/slapd-search.c b/tests/progs/slapd-search.c index 1b7795cca7..5709c7be48 100644 --- a/tests/progs/slapd-search.c +++ b/tests/progs/slapd-search.c @@ -223,6 +223,8 @@ do_random( char *uri, char *manager, struct berval *passwd, char **values = NULL; LDAPMessage *res = NULL, *e = NULL; + srand( pid ); + attrs[ 0 ] = attr; attrs[ 1 ] = NULL; @@ -294,8 +296,12 @@ do_random( char *uri, char *manager, struct berval *passwd, for ( i = 0; i < innerloop; i++ ) { char buf[ BUFSIZ ]; +#if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */ + int r = rand() % nvalues; +#endif + int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0); - snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ rand() % nvalues ] ); + snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] ); do_search( uri, manager, passwd, sbase, buf, &ld, noattrs, 1, maxretries, delay, force, chaserefs ); -- 2.39.5