int main() { // inverse temperature double beta = 1 / T; // random number generator boost::mt19937 eng(SEED); boost::variate_generator<boost::mt19937&, boost::uniform_real<> > random_01(eng, boost::uniform_real<>()); // spin configuration std::vector<int> spins(L); for (int s = 0; s < L; ++s) spins[s] = (random_01() < 0.5 ? 1 : -1); // measurement double ene = 0; double mag = 0; double mag2 = 0; double mag4 = 0; // timer boost::timer tm; for (int mcs = 0; mcs < MCSTEP + MCTHRM; ++mcs) { for (int s = 0; s < L; ++s) { double diff = 2 * spins[s] * (spins[left(s)] + spins[right(s)]); if (random_01() < 0.5 * (1 + std::tanh(-0.5 * beta * diff))) spins[s] = -spins[s]; } if (mcs >= MCTHRM) { double m = 0; double g = 0; for (int s = 0; s < L; ++s) { g -= spins[s] * spins[right(s)]; m += spins[s]; } g /= L; m /= L; ene += g; mag += m; mag2 += m * m; mag4 += std::pow(m, 4); } } // output results std::cout << "Energy = " << ene / MCSTEP << std::endl; std::cout << "Magnetization = " << mag / MCSTEP << std::endl; std::cout << "Magnetization^2 = " << mag2 / MCSTEP << std::endl; std::cout << "Magnetization^4 = " << mag4 / MCSTEP << std::endl; std::cout << "Binder Ratio of Magnetization = " << mag2 * mag2 / mag4 / MCSTEP << std::endl; std::cerr << "Elapsed time = " << tm.elapsed() << " sec\n"; return 0; }
void random_multinomials ( const Vector<float> & weights_in, Vector<uint32_t> & indices_out) { float total = sum(weights_in); ASSERT_LT(0, total); const size_t I = weights_in.size; const size_t N = indices_out.size; const float * restrict weights = weights_in; // this uses a comb-in-bin particle resampling method float dw = N / total; size_t i = 0; float offset = weights[i] * dw - random_01(); typedef Vector<uint32_t>::iterator Auto; for (Auto index = indices_out.begin(); index != indices_out.end(); ++index) { while (offset < 0) { i = (i + 1) % I; offset += weights[i] * dw; } offset -= 1; *index = i; } }
static gboolean tool_random_engine_run_discrete (data_analysis_output_t *dao, tools_data_random_t *info, G_GNUC_UNUSED discrete_random_tool_t *param, discrete_random_tool_local_t **continuity) { gint i; discrete_random_tool_local_t *data = *continuity; for (i = 0; i < info->n_vars; i++) { int k; for (k = 0; k < info->count; k++) { int j; gnm_float x = random_01 (); for (j = 0; data->cumul_p[j] < x; j++) ; dao_set_cell_value (dao, i, k, value_dup (data->values[j])); } } tool_random_engine_run_discrete_clear_continuity (continuity); return FALSE; }
static GnmValue * gnumeric_randuniform (GnmFuncEvalInfo *ei, GnmValue const * const *argv) { gnm_float a = value_get_as_float (argv[0]); gnm_float b = value_get_as_float (argv[1]); if (a > b) return value_new_error_NUM (ei->pos); return value_new_float (a + ( random_01 () * (b - a) ) ); }
static GnmValue * gnumeric_randbetween (GnmFuncEvalInfo *ei, GnmValue const * const *argv) { gnm_float bottom = value_get_as_float (argv[0]); gnm_float top = value_get_as_float (argv[1]); if (bottom > top) return value_new_error_NUM (ei->pos); /* Bottom is ceiled, top floored. Neither is truncated. */ bottom = gnm_ceil (bottom); top = gnm_floor (top); return value_new_float (bottom + gnm_floor ((top + 1.0 - bottom) * random_01 ())); }
static gboolean tool_random_engine_run_uniform (data_analysis_output_t *dao, tools_data_random_t *info, uniform_random_tool_t *param) { int i, n; gnm_float range = param->upper_limit - param->lower_limit; for (i = 0; i < info->n_vars; i++) { for (n = 0; n < info->count; n++) { gnm_float v; v = range * random_01 () + param->lower_limit; dao_set_cell_float (dao, i, n, v); } } return FALSE; }
static void create_animation (AboutState *state) { AboutRenderer *r; GList *tail; unsigned ui; unsigned N = G_N_ELEMENTS (contributors); unsigned *permutation; OVERLAP (-500); r = make_text_item (state, _("Gnumeric is the result of"), 3000); set_text_motion (r, 0.5, 0.9, 0.5, 0.1); tail = state->waiting = g_list_prepend (NULL, r); OVERLAP (2000); r = make_text_item (state, _("the efforts of many people."), 3000); set_text_motion (r, 0.5, 0.9, 0.5, 0.1); APPENDR (r); OVERLAP (2000); r = make_text_item (state, _("Your help is much appreciated!"), 3000); set_text_motion (r, 0.5, 0.9, 0.5, 0.1); APPENDR (r); permutation = g_new (unsigned, N); for (ui = 0; ui < N; ui++) permutation[ui] = ui; for (ui = 0; ui < N; ui++) { unsigned pui = (int)(random_01 () * N); unsigned A = permutation[ui]; permutation[ui] = permutation[pui]; permutation[pui] = A; } for (ui = 0; ui < N; ui++) { unsigned pui = permutation[ui]; const char *name = contributors[pui].name; int style = ui % 2; if (ui != 0) OVERLAP (1900); r = make_text_item (state, name, 3000); switch (style) { case 0: set_text_motion (r, 0.5, 0.1, 0.1, 0.9); if (0) set_text_expansion (r, 1); break; case 1: set_text_motion (r, 0.5, 0.1, 0.9, 0.9); if (0) set_text_expansion (r, 1); break; #if 0 case 2: set_text_motion (r, 0.5, 0.1, 0.5, 0.9); set_text_expansion (r, 3); break; #endif } APPENDR (r); } g_free (permutation); OVERLAP (-1000); r = make_text_item (state, _("We apologize if anyone was left out."), 3000); set_text_motion (r, 0.5, 0.9, 0.5, 0.1); APPENDR (r); OVERLAP (2000); r = make_text_item (state, _("Please contact us to correct mistakes."), 3000); set_text_motion (r, 0.5, 0.9, 0.5, 0.1); APPENDR (r); OVERLAP (2000); r = make_text_item (state, _("Report problems at"), 3000); set_text_motion (r, 0.5, 0.9, 0.5, 0.1); APPENDR (r); OVERLAP (2000); r = make_text_item (state, PACKAGE_BUGREPORT, 3000); set_text_motion (r, 0.5, 0.9, 0.5, 0.1); APPENDR (r); r = make_text_item (state, _("We aim to please!"), 3000); r->fade_out = FALSE; APPENDR (r); OVERLAP (-100); r = make_text_item (state, _("We aim to please!"), 1000); r->fade_in = FALSE; set_text_expansion (r, 4); APPENDR (r); state->now = 0; }
static GnmValue * gnumeric_rand (GnmFuncEvalInfo *ei, GnmValue const * const *argv) { return value_new_float (random_01 ()); }
static GnmValue * gnumeric_randdiscrete (GnmFuncEvalInfo *ei, GnmValue const * const *argv) { GnmValue *res = NULL; gnm_float *values = NULL; gnm_float *probs = NULL; int nv, np, i; gnm_float p; values = collect_floats_value (argv[0], ei->pos, COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS | COLLECT_IGNORE_BLANKS, &nv, &res); if (res) goto out; if (argv[1]) { probs = collect_floats_value (argv[1], ei->pos, COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS | COLLECT_IGNORE_BLANKS, &np, &res); if (res) goto out; } else np = nv; if (nv < 1 || nv != np) goto error; if (probs) { gnm_float pmin, psum; gnm_range_min (probs, np, &pmin); if (pmin < 0) goto error; gnm_range_sum (probs, np, &psum); if (gnm_abs (psum - 1) > 1e-10) goto error; } p = random_01 (); if (probs) { for (i = 0; i < np; i++) { p -= probs[i]; if (p < 0) break; } } else { /* Uniform. */ i = (int)gnm_floor (p * nv); } /* MIN is needed because of the sum grace. */ res = value_new_float (values[MIN (i, nv - 1)]); out: g_free (values); g_free (probs); return res; error: res = value_new_error_NUM (ei->pos); goto out; }