int main(int argc, char *argv[]) { size_t niters = 1000; size_t right_shift = 0; uint64_t mask = 0xffff; int argno=1; for (/*void*/; argno<argc && '-'==argv[argno][0]; ++argno) { if (!strcmp(argv[argno], "--")) { ++argno; break; } else if (!strncmp(argv[argno], "--total=", 8)) { niters = strtoul(argv[argno]+8, NULL, 0); } else if (!strncmp(argv[argno], "--shift=", 8)) { right_shift = strtoul(argv[argno]+8, NULL, 0); } else if (!strncmp(argv[argno], "--mask=", 7)) { mask = strtoul(argv[argno]+7, NULL, 0); } else { std::cerr <<argv[0] <<": unrecognized switch: " <<argv[argno] <<"\n"; exit(1); } } uint64_t prev = IntegerOps::shiftRightLogical2(next_random(), right_shift) & mask; for (size_t i=0; i<niters; ++i) { uint64_t next = IntegerOps::shiftRightLogical2(next_random(), right_shift) & mask; std::cout <<prev <<"\t" <<next <<"\n"; prev = next; } return 0; }
static void load_colors(float *colors){ int tri, point, j, k, l; float r = next_random() * 0.5 + 0.5; float g = next_random() * 0.5 + 0.5; float b = next_random() * 0.5 + 0.5; for (tri = 0; tri < 12; tri++) { for (point = 0; point < 3; point++) { j = tri * 9 + point * 3 + 0; k = tri * 9 + point * 3 + 1; l = tri * 9 + point * 3 + 2; colors[j] = r; colors[k] = g; colors[l] = b; } } }
static circle_t gen_circle(int i) { int clr; circle_t c; c.x = 0.2 + sin(i*0.01)*2.0 + next_random()*0.1; c.y = -0.2 + cos(i*0.001)*0.5 + next_random()*0.1; c.r = 0.005 + next_random()*0.03; c.t = 0.0; c.dx = 0.03 * sin(next_random()); c.dy = -0.01 * cos(next_random()); clr = rand()%4; switch (clr) { case 0: c.fill = color(0x3fa080cf); break; case 1: c.fill = color(0x3fb9b5fc); break; case 2: c.fill = color(0x0f784f56); break; default: case 3: c.fill = color(0x0fb1103c); break; }; c.stroke = color(0x3f705391); return c; }
double rng::get_random_double() { union { double floating_point_number; uint64_t integer; } number; number.integer = 0u; /* Exponent. It's set to zero. Exponent bias is 1023 in double precision, and therefore the value 1023 needs to be encoded. */ number.integer |= static_cast<uint64_t>(1023) << 52; /* Significand. A double-precision floating point number stores 52 significand bits. The underlying RNG only gives us 32 bits, so we need to shift the bits 20 positions to the left. The last 20 significand bits we can leave at zero, we don't need the full 52 bits of randomness allowed by the double-precision format. */ number.integer |= static_cast<uint64_t>(next_random()) << (52 - 32); /* At this point, the exponent is zero. The significand, taking into account the implicit leading one bit, is at least exactly one and at most almost two. In other words, interpreting the value as a double gives us a number in the range [1, 2[. Simply subtract one from that value and return it. */ return number.floating_point_number - 1.0; }
void particle_system_init(ParticleSystem *sys){ sys->num_cubes = sys->num_particles + 1; sys->num_triangles = 6 * 2 * 3 * sys->num_cubes; sys->num_floats = 6 * 2 * 3 * 3 * sys->num_cubes; sys->attractor_size = 0.03; sys->particle_size = 0.003; sys->attractor[0] = 0; sys->attractor[1] = 0; sys->attractor[2] = 0; int i; load_cubes(sys->attractor, sys->cubes, sys->attractor_size); for (i = 0; i < 3 * 3 * 12; i++) { sys->colors[i] = 1.0; //want the cube to be all white } int j,k,l; for(i = 0;i < sys->num_particles;i++){ j = i * 3 + 0; k = i * 3 + 1; l = i * 3 + 2; sys->positions[j] = next_random(); sys->positions[k] = next_random(); sys->positions[l] = next_random(); load_cubes(sys->positions + j, sys->cubes + (i+1) * 3 * 36, sys->particle_size); load_colors(sys->colors + (i+1) * 3 * 36); sys->velocities[j] = next_random(); sys->velocities[k] = next_random(); sys->velocities[l] = next_random(); sys->accelerations[j] = 0.0; sys->accelerations[k] = 0.0; sys->accelerations[l] = 0.0; } sys->friction_coeff = 0.25; }
/** * This code is based on the boost implementation of uniform_smallint. * http://www.boost.org/doc/libs/1_55_0/boost/random/uniform_smallint.hpp * Using that code would be ideal, except that boost, and C++11, do not * guarantee that it will work the same way on all platforms, or that the * results may not be different in future versions of the library. * The simplified version I have written should work the same on all * platforms, which is the most important thing for us. * The existence of "modulo bias" seems less important when we have moved * to boost::mt19937, since it guarantees that there are no "bad bits" * and has a very large range. * * If a standard cross platform version becomes available then this should * be replaced. */ int rng::get_random_int_in_range_zero_to(int max) { assert(max >= 0); return static_cast<int> (next_random() % (static_cast<uint32_t>(max)+1)); }
/* * mk_item */ int mk_w_item (void* row, ds_key_t index) { int32_t res = 0; /* begin locals declarations */ decimal_t dMinPrice, dMaxPrice, dMarkdown; static decimal_t dMinMarkdown, dMaxMarkdown; int32_t bUseSize, bFirstRecord = 0, nFieldChangeFlags, nMin, nMax, nIndex, nTemp; char *cp; struct W_ITEM_TBL *r; static int32_t bInit = 0; struct W_ITEM_TBL *rOldValues = &g_OldValues; char *szMinPrice = NULL, *szMaxPrice = NULL; tdef *pT = getSimpleTdefsByNumber(ITEM); if (row == NULL) r = &g_w_item; else r = row; if (!bInit) { /* some fields are static throughout the data set */ strtodec(&dMinMarkdown, MIN_ITEM_MARKDOWN_PCT); strtodec(&dMaxMarkdown, MAX_ITEM_MARKDOWN_PCT); bInit = 1; } memset(r, 0, sizeof(struct W_ITEM_TBL)); /* build the new value */ nullSet(&pT->kNullBitMap, I_NULLS); r->i_item_sk = index; nIndex = pick_distribution(&nMin, "i_manager_id", 2, 1, I_MANAGER_ID); dist_member(&nMax, "i_manager_id", nIndex, 3); genrand_key(&r->i_manager_id, DIST_UNIFORM, (ds_key_t)nMin, (ds_key_t)nMax, 0, I_MANAGER_ID); /* if we have generated the required history for this business key and generate a new one * then reset associated fields (e.g., rec_start_date minimums) */ if (setSCDKeys(I_ITEM_ID, index, r->i_item_id, &r->i_rec_start_date_id, &r->i_rec_end_date_id)) { /* * some fields are not changed, even when a new version of the row is written */ bFirstRecord = 1; } /* * this is where we select the random number that controls if a field changes from * one record to the next. */ nFieldChangeFlags = next_random(I_SCD); /* the rest of the record in a history-keeping dimension can either be a new data value or not; * use a random number and its bit pattern to determine which fields to replace and which to retain */ gen_text (r->i_item_desc, 1, RS_I_ITEM_DESC, I_ITEM_DESC); changeSCD(SCD_CHAR, &r->i_item_desc, &rOldValues->i_item_desc, &nFieldChangeFlags, bFirstRecord); nIndex = pick_distribution(&szMinPrice, "i_current_price", 2, 1, I_CURRENT_PRICE); dist_member(&szMaxPrice, "i_current_price", nIndex, 3); strtodec(&dMinPrice, szMinPrice); strtodec(&dMaxPrice, szMaxPrice); genrand_decimal(&r->i_current_price, DIST_UNIFORM, &dMinPrice, &dMaxPrice, NULL, I_CURRENT_PRICE); changeSCD(SCD_INT, &r->i_current_price, &rOldValues->i_current_price, &nFieldChangeFlags, bFirstRecord); genrand_decimal(&dMarkdown, DIST_UNIFORM, &dMinMarkdown, &dMaxMarkdown, NULL, I_WHOLESALE_COST); decimal_t_op(&r->i_wholesale_cost, OP_MULT, &r->i_current_price, &dMarkdown); changeSCD(SCD_DEC, &r->i_wholesale_cost, &rOldValues->i_wholesale_cost, &nFieldChangeFlags, bFirstRecord); hierarchy_item (I_CATEGORY, &r->i_category_id, &r->i_category, index); /* * changeSCD(SCD_INT, &r->i_category_id, &rOldValues->i_category_id, &nFieldChangeFlags, bFirstRecord); */ hierarchy_item (I_CLASS, &r->i_class_id, &r->i_class, index); changeSCD(SCD_KEY, &r->i_class_id, &rOldValues->i_class_id, &nFieldChangeFlags, bFirstRecord); cp = &r->i_brand[0]; hierarchy_item (I_BRAND, &r->i_brand_id, &cp, index); changeSCD(SCD_KEY, &r->i_brand_id, &rOldValues->i_brand_id, &nFieldChangeFlags, bFirstRecord); /* some categories have meaningful sizes, some don't */ if (r->i_category_id) { dist_member(&bUseSize, "categories", (int)r->i_category_id, 3); pick_distribution (&r->i_size, "sizes", 1, bUseSize + 2, I_SIZE); changeSCD(SCD_PTR, &r->i_size, &rOldValues->i_size, &nFieldChangeFlags, bFirstRecord); } else { bUseSize = 0; r->i_size = NULL; } nIndex = pick_distribution(&nMin, "i_manufact_id", 2, 1, I_MANUFACT_ID); genrand_integer(&nTemp, DIST_UNIFORM, nMin, dist_member(NULL, "i_manufact_id", nIndex, 3), 0, I_MANUFACT_ID); r->i_manufact_id = nTemp; changeSCD(SCD_KEY, &r->i_manufact_id, &rOldValues->i_manufact_id, &nFieldChangeFlags, bFirstRecord); mk_word (r->i_manufact, "syllables", (int) r->i_manufact_id, RS_I_MANUFACT, ITEM); changeSCD(SCD_CHAR, &r->i_manufact, &rOldValues->i_manufact, &nFieldChangeFlags, bFirstRecord); gen_charset(r->i_formulation, DIGITS, RS_I_FORMULATION, RS_I_FORMULATION, I_FORMULATION); embed_string(r->i_formulation, "colors", 1, 2, I_FORMULATION); changeSCD(SCD_CHAR, &r->i_formulation, &rOldValues->i_formulation, &nFieldChangeFlags, bFirstRecord); pick_distribution (&r->i_color, "colors", 1, 2, I_COLOR); changeSCD(SCD_PTR, &r->i_color, &rOldValues->i_color, &nFieldChangeFlags, bFirstRecord); pick_distribution (&r->i_units, "units", 1, 1, I_UNITS); changeSCD(SCD_PTR, &r->i_units, &rOldValues->i_units, &nFieldChangeFlags, bFirstRecord); pick_distribution (&r->i_container, "container", 1, 1, ITEM); changeSCD(SCD_PTR, &r->i_container, &rOldValues->i_container, &nFieldChangeFlags, bFirstRecord); mk_word (r->i_product_name, "syllables", (int) index, RS_I_PRODUCT_NAME, ITEM); r->i_promo_sk = mk_join(I_PROMO_SK, PROMOTION, 1); genrand_integer(&nTemp, DIST_UNIFORM, 1, 100, 0, I_PROMO_SK); if (nTemp > I_PROMO_PERCENTAGE) r->i_promo_sk = -1; /* * if this is the first of a set of revisions, then baseline the old values */ if (bFirstRecord) memcpy(&g_OldValues, r, sizeof(struct W_ITEM_TBL)); if (index == 1) memcpy(&g_OldValues, r, sizeof(struct W_ITEM_TBL)); return (res); }