示例#1
0
/*
 * Choose a seed based on the best random input available.  Prefers
 * /dev/random as a source of random numbers, and reads the entire
 * 624-int state from that device.  Because of this approach, the
 * function can take a long time (in real time) to complete, since
 * /dev/random may have to wait quite a while before it can provide
 * that much randomness.  If /dev/random is unavailable, falls back to
 * calling mts_goodseed.
 */
void mts_bestseed(
    mt_state*		state)		/* State vector to seed */
    {
    int			bytesread;	/* Byte count read from device */
    int			nextbyte;	/* Index of next byte to read */
    FILE*		ranfile;	/* Access to device */

    ranfile = fopen("/dev/random", "rb");
    if (ranfile == NULL)
	{
	mts_goodseed(state);
	return;
	}

    for (nextbyte = 0;
      nextbyte < (int)sizeof state->statevec;
      nextbyte += bytesread)
	{
	bytesread = fread((char *)&state->statevec + nextbyte, 1,
	  sizeof state->statevec - nextbyte, ranfile);
	if (bytesread == 0)
	    {
	    /*
	     * Something went wrong.  Fall back to time-based seeding.
	     */
	    fclose(ranfile);
	    mts_goodseed(state);
	    return;
	    }
	}
    }
示例#2
0
void *cvar_alloc_handle(const char *cvar_parameters,
		void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr))
{
	cvar_token_t *list_head;;
	cvar_token_t *t;
	handle_t handle;
	handle_t *state = NULL;
	int ret = 0;

	cvar_trace("entry");

	/* Tokenize parameters supplied by filebench. */
	list_head = NULL;
	ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER,
			DEFAULT_KEY_VALUE_DELIMITER, &list_head);
	if (ret)
		goto out;

	/* Get the value of mean and sigma. */
	t = find_token(list_head, RN_MEAN);
	if (t && t->value) {
		t->used = 1;
		handle.mean = atof(t->value);
	} else
		handle.mean = RN_MEAN_DEFAULT;

	t = find_token(list_head, RN_SIGMA);
	if (t && t->value) {
		t->used = 1;
		handle.sigma = atof(t->value);
	} else
		handle.sigma = RN_SIGMA_DEFAULT;

	cvar_trace("mean = %lf, sigma = %lf", handle.mean, handle.sigma);

	t = unused_tokens(list_head);
	if (t) {
		cvar_log_error("Unsupported parameter %s", t->key);
		goto out;
	}

	/* Seed the state. */
	mts_goodseed(&handle.state);

	/* All set. Now allocate space for the handle in the shared segment and
	 * copy the state over. */
	state = (handle_t *) cvar_malloc(sizeof(handle_t));
	if (!state) {
		cvar_log_error("Out of memory");
		goto out;
	}

	*state = handle;

out:
	free_tokens(list_head);

	cvar_trace("exit");
	return state;
}
示例#3
0
/*
 * Initialize the PRNG from random input.  See mts_goodseed.
 */
uint32_t mt_goodseed(void)
    {
    return mts_goodseed(&mt_default_state);
    }
示例#4
0
文件: sampler.c 项目: bherila/polaris
Sampler *createSampler() {
    Sampler *to_return = (Sampler*)malloc(sizeof (Sampler));
    mts_goodseed(&to_return->mState);
    return to_return;
}
示例#5
0
/*
 * Initialize the PRNG from random input.  See mts_goodseed.
 */
void mt_goodseed()
    {
    mts_goodseed(&mt_default_state);
    }
示例#6
0
void *cvar_alloc_handle(const char *cvar_parameters,
		void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr))
{
	cvar_token_t *list_head;;
	cvar_token_t *t;
	handle_t handle;
	handle_t *state = NULL;
	int ret = 0;

	cvar_trace("entry");

	/* Tokenize parameters supplied by filebench. */
	list_head = NULL;
	ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER,
			DEFAULT_KEY_VALUE_DELIMITER, &list_head);
	if (ret)
		goto out;

	/* Get the value of lower, upper and mode. */
	t = find_token(list_head, RT_LOWER);
	if (t && t->value) {
		t->used = 1;
		handle.lower = atof(t->value);
	} else
		handle.lower = RT_LOWER_DEFAULT;

	t = find_token(list_head, RT_UPPER);
	if (t && t->value) {
		t->used = 1;
		handle.upper = atof(t->value);
	} else
		handle.upper = RT_UPPER_DEFAULT;

	t = find_token(list_head, RT_MODE);
	if (t && t->value) {
		t->used = 1;
		handle.mode = atof(t->value);
	} else
		handle.mode = RT_MODE_DEFAULT;

	cvar_trace("lower = %lf, upper = %lf, mode = %lf", handle.lower,
			handle.upper, handle.mode);

	/* Validate parameters. */
	if (handle.upper < handle.lower) {
		cvar_log_error("Invalid parameter values: lower = %lf and upper = %lf. "
				"upper must be greater than lower", handle.lower, handle.upper);
		goto out;
	}

	if ((handle.mode > handle.upper) || (handle.mode < handle.lower)) {
		cvar_log_error("Invalid parameter values: lower = %lf, mode = %lf and "
				"upper = %lf. mode must be between lower and upper",
				handle.lower, handle.mode, handle.upper);
		goto out;
	}

	/* Check if there are unused tokens. */
	t = unused_tokens(list_head);
	if (t) {
		cvar_log_error("Unsupported parameter %s", t->key);
		goto out;
	}

	/* Seed the state. */
	mts_goodseed(&handle.state);

	/* All set. Now allocate space for the handle in the shared segment and
	 * copy the state over. */
	state = (handle_t *) cvar_malloc(sizeof(handle_t));
	if (!state) {
		cvar_log_error("Out of memory");
		goto out;
	}

	*state = handle;

out:
	free_tokens(list_head);

	cvar_trace("exit");
	return state;
}
示例#7
0
void *cvar_alloc_handle(const char *cvar_parameters,
		void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr))
{
	cvar_token_t *list_head;;
	cvar_token_t *t;
	handle_t handle;
	handle_t *state = NULL;
	int ret = 0;

	cvar_trace("entry");

	/* Tokenize parameters supplied by filebench. */
	list_head = NULL;
	ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER,
			DEFAULT_KEY_VALUE_DELIMITER, &list_head);
	if (ret)
		goto out;

	/* Get the value of shape and rate. */
	t = find_token(list_head, RER_SHAPE);
	if (t && t->value) {
		t->used = 1;
		handle.shape = atoi(t->value);
	} else
		handle.shape = RER_SHAPE_DEFAULT;

	t = find_token(list_head, RER_RATE);
	if (t && t->value) {
		t->used = 1;
		handle.rate = atof(t->value);
	} else
		handle.rate = RER_RATE_DEFAULT;

	cvar_trace("shape = %d, rate = %lf", handle.shape, handle.rate);

	/* Validate parameters. */
	if (handle.shape < 0) {
		cvar_log_error("Invalid parameter value: shape = %d. shape is a "
				"non-zero positive integer", handle.shape);
		goto out;
	}

	if (handle.rate < 0) {
		cvar_log_error("Invalid parameter value: rate = %lf. rate is a "
				"non-zero positive rational number", handle.rate);
		goto out;
	}

	t = unused_tokens(list_head);
	if (t) {
		cvar_log_error("Unsupported parameter %s", t->key);
		goto out;
	}

	/* Seed the state. */
	mts_goodseed(&handle.state);

	/* All set. Now allocate space for the handle in the shared segment and
	 * copy the state over. */
	state = (handle_t *) cvar_malloc(sizeof(handle_t));
	if (!state) {
		cvar_log_error("Out of memory");
		goto out;
	}

	*state = handle;

out:
	free_tokens(list_head);

	cvar_trace("exit");
	return state;
}