int main(int argc, char* argv[]) { double tx, ty, tz; double volume; int successes=0, attempts=0; setCommandLineParameters(argc, argv); if (getFlagParam("-usage")) { printf("usage: cv [ -randomize ]\n\n"); printf(" will return MC volume of list of spheres entered, box size determined automatically.\n"); printf("\n"); printf("// Reads a centered cluster and determines the volume of it.\n"); printf("// Will deliver an erroneous result if cluster is not centered or percolates.\n"); printf("\n"); printf("// More generally, it will give the volume of all the cavities, whether clustered or not.\n"); printf("\n"); printf("// In: one cluster; 1 or more records in .cav format \n"); printf("// Out: .dst (reports one value of volume) \n"); exit(1); } if (getFlagParam("-randomize")) initializeRandomNumberGenerator2(-1); else initializeRandomNumberGenerator2(0); instream = stdin; readInputStream(); findMinimumBoxSize(); successes=0; for (attempts=0; successes<N_SUCCESSES; attempts++) { // pick a point and check inclusion tx = rnd2() * box_x; ty = rnd2() * box_y; tz = rnd2() * box_z; successes += checkInclusion(tx, ty, tz); if (attempts > 100000000) { printf("too many attempts."); exit(1); } } volume = (box_x * box_y * box_z * successes)/attempts; printf("%lf\n", volume); return 0; }
int RandomBuf::readFromDevice(char* buffer, std::streamsize length) { int n = 0; #if defined(POCO_OS_FAMILY_WINDOWS) HCRYPTPROV hProvider = 0; CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); CryptGenRandom(hProvider, (DWORD) length, (BYTE*) buffer); CryptReleaseContext(hProvider, 0); n = static_cast<int>(length); #else #if defined(POCO_OS_FAMILY_UNIX) int fd = open("/dev/urandom", O_RDONLY, 0); if (fd >= 0) { n = read(fd, buffer, length); close(fd); } #endif if (n <= 0) { // x is here as a source of randomness, so it does not make // much sense to protect it with a Mutex. static UInt32 x = 0; Random rnd1(256); Random rnd2(64); x += rnd1.next(); n = 0; SHA1Engine engine; UInt32 t = (UInt32) std::time(NULL); engine.update(&t, sizeof(t)); void* p = this; engine.update(&p, sizeof(p)); engine.update(buffer, length); UInt32 junk[32]; engine.update(junk, sizeof(junk)); while (n < length) { for (int i = 0; i < 100; ++i) { UInt32 r = rnd2.next(); engine.update(&r, sizeof(r)); engine.update(&x, sizeof(x)); x += rnd1.next(); } DigestEngine::Digest d = engine.digest(); for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n < length; ++it, ++n) { engine.update(*it); *buffer++ = *it++; } } } #endif return n; }
int main(int argc, char* argv[]) { double surface_area; int successes=0, attempts=0; int particle_number; instream=stdin; setCommandLineParameters(argc, argv); getVectorParam("-box", &box_x, &box_y, &box_z); if (getFlagParam("-randomize")) initializeRandomNumberGenerator2(-1); else initializeRandomNumberGenerator2(0); readInputStream(); // figure out total surface area for sampling for (particle_number=0; particle_number<number_of_cavities; particle_number++) { sorface_area[particle_number] = PI * d[particle_number] * d[particle_number]; total_surface_area += sorface_area[particle_number]; } for (attempts=0; attempts<N_SAMPLES; attempts++) { // pick a particle, then pick a point on its surface while(1) { particle_number = rnd2() * number_of_cavities; if (rnd2() < (sorface_area[particle_number]/total_surface_area)) break; } generateSamplePoint(particle_number); successes += checkExclusion(); } // volume = (box_x * box_y * box_z * successes)/attempts; surface_area = (total_surface_area * successes)/attempts; printf("%lf\n", surface_area); return 0; }
void generateSamplePoint(int particle_number) { double sin_theta, cos_theta, sin_phi, cos_phi; double radius; sample_theta = 0; while (rnd2() >= (sin_theta = sin(sample_theta))) sample_theta = rnd2() * PI; sample_phi = rnd2() * 2 * PI; cos_theta = cos(sample_theta); sin_phi = sin(sample_phi); cos_phi = cos(sample_phi); radius = d[particle_number] * .5; sample_x = x[particle_number] + radius * sin_theta * cos_phi; sample_y = y[particle_number] + radius * sin_theta * sin_phi; sample_z = z[particle_number] + radius * cos_theta; printf("%lf\t%lf\t%lf\t%lf\t1\n", sample_x, sample_y, sample_z, .1); }
int main(int argc, char* argv[]) { double tx, ty, tz; double volume; int successes=0, attempts=0; setCommandLineParameters(argc, argv); getVectorParam("-box", &box_x, &box_y, &box_z); if (getFlagParam("-randomize")) initializeRandomNumberGenerator2(-1); else initializeRandomNumberGenerator2(0); readInputStream(); successes=0; for (attempts=0; successes<N_SUCCESSES; attempts++) { // pick a point and check inclusion tx = rnd2() * box_x; ty = rnd2() * box_y; tz = rnd2() * box_z; successes += checkInclusion(tx, ty, tz); if (attempts > 100000000) { printf("too many attempts."); exit(1); } } volume = (box_x * box_y * box_z * successes)/attempts; printf("%lf\n", volume); return 0; }