1999-10-20 23:25:17 +00:00
|
|
|
/*
|
2000-02-03 23:08:31 +00:00
|
|
|
* Copyright (C) 1999, 2000 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
1999-10-31 18:42:01 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-07-27 09:55:03 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
|
|
|
|
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
|
|
|
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
|
|
|
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1999-10-20 23:25:17 +00:00
|
|
|
*/
|
1999-10-31 18:42:01 +00:00
|
|
|
|
2000-09-26 17:23:19 +00:00
|
|
|
/* $Id: random.c,v 1.13 2000/09/26 17:23:17 gson Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1999-10-20 23:25:17 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/once.h>
|
|
|
|
#include <isc/random.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1999-10-20 23:25:17 +00:00
|
|
|
|
|
|
|
static isc_once_t once = ISC_ONCE_INIT;
|
|
|
|
|
|
|
|
static void
|
|
|
|
initialize_rand(void)
|
|
|
|
{
|
2000-09-06 02:40:00 +00:00
|
|
|
srand(time(NULL));
|
1999-10-20 23:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
initialize(void)
|
|
|
|
{
|
|
|
|
RUNTIME_CHECK(isc_once_do(&once, initialize_rand) == ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-09-06 02:40:00 +00:00
|
|
|
isc_random_seed(isc_uint32_t seed)
|
1999-10-20 23:25:17 +00:00
|
|
|
{
|
|
|
|
initialize();
|
|
|
|
|
|
|
|
srand(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-09-06 02:40:00 +00:00
|
|
|
isc_random_get(isc_uint32_t *val)
|
1999-10-20 23:25:17 +00:00
|
|
|
{
|
|
|
|
REQUIRE(val != NULL);
|
|
|
|
|
|
|
|
initialize();
|
|
|
|
|
|
|
|
*val = rand();
|
2000-09-06 02:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_uint32_t
|
2000-09-26 17:23:19 +00:00
|
|
|
isc_random_jitter(isc_uint32_t max, isc_uint32_t jitter) {
|
|
|
|
REQUIRE(jitter < max);
|
2000-09-08 00:06:39 +00:00
|
|
|
if (jitter == 0)
|
2000-09-26 17:23:19 +00:00
|
|
|
return (max);
|
|
|
|
else
|
|
|
|
return (max - rand() % jitter);
|
1999-10-20 23:25:17 +00:00
|
|
|
}
|