initialize random seed; use high-order bits for better randomness

This commit is contained in:
Pierangelo Masarati 2006-08-25 15:15:13 +00:00
parent ca262ee8e6
commit c838ce513d
3 changed files with 24 additions and 8 deletions

View file

@ -288,7 +288,7 @@ do_base( char *uri, char *dn, struct berval *pass, char *base, char *filter, cha
int version = LDAP_VERSION3; int version = LDAP_VERSION3;
char *nullstr = ""; char *nullstr = "";
srand(pid); srand( pid );
ldap_initialize( &ld, uri ); ldap_initialize( &ld, uri );
if ( ld == NULL ) { if ( ld == NULL ) {
@ -397,16 +397,19 @@ novals:;
/* Ok, got list of DNs, now start binding to each */ /* Ok, got list of DNs, now start binding to each */
for ( i = 0; i < maxloop; i++ ) { for ( i = 0; i < maxloop; i++ ) {
int j, k; int j;
struct berval cred = { 0, NULL }; 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] ) ) { if ( creds && !BER_BVISEMPTY( &creds[j] ) ) {
cred = creds[j]; cred = creds[j];
} }
if ( do_bind( uri, dns[j], &cred, 1, force, chaserefs, noinit, &ld ) if ( do_bind( uri, dns[j], &cred, 1, force, chaserefs, noinit, &ld )
&& !force ) && !force )
{ {

View file

@ -215,6 +215,8 @@ do_random( char *uri, char *manager, struct berval *passwd,
char **values = NULL; char **values = NULL;
LDAPMessage *res = NULL, *e = NULL; LDAPMessage *res = NULL, *e = NULL;
srand( pid );
attrs[ 0 ] = LDAP_NO_ATTRS; attrs[ 0 ] = LDAP_NO_ATTRS;
attrs[ 1 ] = NULL; attrs[ 1 ] = NULL;
@ -275,7 +277,12 @@ do_random( char *uri, char *manager, struct berval *passwd,
} }
for ( i = 0; i < innerloop; i++ ) { 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, noattrs, 1, maxretries, delay, force,
chaserefs ); chaserefs );
} }

View file

@ -223,6 +223,8 @@ do_random( char *uri, char *manager, struct berval *passwd,
char **values = NULL; char **values = NULL;
LDAPMessage *res = NULL, *e = NULL; LDAPMessage *res = NULL, *e = NULL;
srand( pid );
attrs[ 0 ] = attr; attrs[ 0 ] = attr;
attrs[ 1 ] = NULL; attrs[ 1 ] = NULL;
@ -294,8 +296,12 @@ do_random( char *uri, char *manager, struct berval *passwd,
for ( i = 0; i < innerloop; i++ ) { for ( i = 0; i < innerloop; i++ ) {
char buf[ BUFSIZ ]; 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, do_search( uri, manager, passwd, sbase, buf, &ld, noattrs,
1, maxretries, delay, force, chaserefs ); 1, maxretries, delay, force, chaserefs );