NUMBER generate_prime(int len, int prob) { NUMBER a_three,a_four; NUMBER prim ; int i ; a_add( &a_one, &a_two, &a_three ); a_add( &a_two, &a_two, &a_four ); init_rnd(); do { gen_number( len, &prim ); } while ( !prim.n_len ); a_mult( &prim, &a_two, &prim ); a_mult( &prim, &a_three, &prim ); a_add( &prim, &a_one, &prim ); for (i=1 ;; i++) { if (p_prim( &prim, prob )) break; if (i % 2) a_add( &prim, &a_four, &prim ); else a_add( &prim, &a_two, &prim ); } //num_fput( &prim, stdout ); return prim ; }
int main(int argc, char **argv) { double T=0;; double mag=0.0; int N; int i; Ising **S; double tot_Erg=0; double capacity=0.0; double kai=0.0; init_rnd(gus()); N=atoi(argv[1]); S=(Ising **)malloc(sizeof(Ising *) * N); for(i=0; i<N; i++) S[i]=(Ising *)malloc(sizeof(Ising) * N); initialize(N, S); for(T=3.01; T>=0.01; T-=0.01) { mag=0.0; tot_Erg=0.0; kai=0.0; capacity=0.0; for(i=0; i<200000; i++) mcstep(N, T, S); for(i=0; i<200000; i++) { mcstep(N, T, S); mag+=magnetization(N, S); tot_Erg+=energy(N, S); kai+=pow(magnetization(N, S), 2); capacity+=pow(energy(N,S), 2); } mag=mag/200000; tot_Erg=tot_Erg/200000; kai=kai/200000; capacity=capacity/200000; kai=(kai-mag*mag)/T; capacity=(capacity - tot_Erg*tot_Erg)/(T*T); kai=kai*N*N; capacity=capacity*N*N; printf("%lf\t%lf\t%lf\t%lf\t%lf\n", T, mag, tot_Erg, kai, capacity); } free(S); return 0; }
int generate_key(NUMBER p1, NUMBER p2, NUMBER_SET *g_public_key, NUMBER_SET *g_secret_key) { NUMBER n,d,e,phi,*max_p; int len; if ( !a_cmp( &p1, &p2 ) ) { //fprintf(stderr,"the prime numbers must not be identical!\n"); //exit(1); return -1 ; } if (a_cmp( &p1, &p2 ) > 0) max_p = &p1; else max_p = &p2; a_mult( &p1, &p2, &n ); a_sub( &p1, &a_one, &phi ); a_sub( &p2, &a_one, &e ); a_mult( &phi, &e, &phi ); len = n_bitlen( &phi ); len = ( len + 3 ) / 4; a_assign( &p1, &phi ); a_sub( &p1, &a_one, &p1 ); init_rnd(); do { do { gen_number( len, &d ); } while (a_cmp( &d, max_p ) <= 0 || a_cmp( &d, &p1 ) >= 0); a_ggt( &d, &phi, &e ); } while ( a_cmp( &e, &a_one ) ); inv( &d, &phi, &e ); // public key a_assign(&(g_public_key->n_key1), &n) ; a_assign(&(g_public_key->n_key2), &e) ; // secret key a_assign(&(g_secret_key->n_key1), &n) ; a_assign(&(g_secret_key->n_key2), &d) ; return 0 ; }
int main() { int i, j; static char s[N]; init_rnd((unsigned long) time(NULL)); for (i = N - M; i < N; i++) { j = (int)((i + 1) * rnd()); /* 0 <= j <= i */ if (s[j] == 0) s[j] = 1; else s[i] = 1; } for (i = 0; i < N; i++) if (s[i]) printf(" %4d", i + 1); printf("\n"); return EXIT_SUCCESS; }
void path_random_generator_t::init(int size, int seed) { init_rnd(m_impl->m_rnd_state, size, seed); }
path_random_generator_t::path_random_generator_t(int size, int seed) : m_impl(new Impl()) { init_rnd(m_impl->m_rnd_state, size, seed); }
void main() { unsigned char i; unsigned char savemode; // // Core Hardware Initialization mcu_initialize(); // Initialize MCU resources and 8KHz interrupt... nvreadbuf(); // Load the NV buffer from flash (get badge Addr and properties) proc_btn1(); // Process Buttons sound_config_polled(); // Configure the sound subsystem chip etoh_init(); // Initialize the Alcohol Sensor Subsystem MRF49XA_Init(); // Initialize the RF tranceiver usb_ser_init(); // Initialize the serial port light_init(); // // Preliminary setup and interaction // These setup a precurser items will occur prior to going into the main worker loop. // This is mainly the one-time powerup and "boot" sequence // // Show the address of the badge on the LEDs (since there are only 7 LED's // the binary number will show in green if the badge is < 128 and red if > // 128 with the badge addresses 0 and 128 showing up as nothing (typically illegal // numbers anyway // Note, led_showbin does not interact with the interrupt driven light show well. // So only use if you may not be in interrupt mode yet of if a show is not running delay_s(1); led_showbin(LED_SHOW_RED, nvget_badgetype() | 0x40); // Startup Indicator delay_10us(100); led_showbin(LED_SHOW_RED, 0); delay_s(1); led_showbin(LED_SHOW_RED, nvget_badgeperm() | 0x40); // Same delay_10us(100); led_showbin(LED_SHOW_RED, 0); delay_s(1); // // Shows address as described above // MyBadgeID = nvget_badgeid(); led_showbin(MyBadgeID & 0x80 ? LED_SHOW_RED : LED_SHOW_GRN, MyBadgeID & 0x7f ); delay_s(TIME_ADDR); led_showbin(LED_SHOW_NONE, 0); init_rnd(nvget_badgetype()+1, MyBadgeID, 0x55); // Seed tha random number generator // // Perform POV credits (spin the badge on your wrist :) // led_pov(LED_SHOW_AUTO, TIME_POV); // Enable Global Interrupts // Use Interrupts. ISR is interrupt(), below intcon.GIE=1; //sample_play(); //delay_s(2); MyMode = MODE_IDLE; modelights(); // Sets lightshow based on a few state parameters // Calculate initial elevation time elevatemsecs = elapsed_msecs + ELEVATE_BASE + (unsigned long) rnd_randomize() * ELEVATE_VAR; // // Main Worker Loop // while(1) { // // Handle the elapsed time clocks // clear_bit(intcon, TMR0IE); loop_msecs = intr_msecs; // Take copy to avoid race conditions intr_msecs = 0; set_bit(intcon, TMR0IE); elapsed_msecs += loop_msecs; // Update elapsed time in msecs savemode = MyMode; // Used to detact mode changes when command processers set new mode switch(MyMode) { case MODE_IDLE: // If button pressed, handle it if(btn_commandwork(loop_msecs) == BTN_WORKING) { MyMode = MODE_GETCMD; modelights(); playsong = 0; //light_pause(); break; } // Look for incoming RF packet if(MRF49XA_Receive_Packet(rfrxbuf,&rfrxlen) == PACKET_RECEIVED) { MRF49XA_Reset_Radio(); //led_showbin(LED_SHOW_RED, 2); //delay_10us(10); //led_showbin(LED_SHOW_RED, 0); rfcmd_execute(rfrxbuf, rfrxlen); } if(savemode != MyMode) { // Don't process remainder of idle switch if state change break; } // If time to send a beacon, send it. Also process quorum sounds. if(elapsed_msecs > (last_beacon + (unsigned long)BEACON_BASE + ((unsigned long)rnd_randomize()* BEACON_RNDSCALE))) { // time for beacon rfcmd_3send(RFCMD_BEACON, MyBadgeID, nvget_socvec1()); last_beacon = elapsed_msecs; rfcmd_clrcden(); if(have_quorum) { if(rfcmd_getdensity() <= DENSITY_QUORUM_LO) { have_quorum = 0; if(!playsong) tune_startsong(SONG_CHIRP2); } } else { if(rfcmd_getdensity() >= DENSITY_QUORUM_HI) { have_quorum = 1; if (!playsong) tune_startsong(SONG_303); } } modelights(); } break; case MODE_ETOH: // Run the alcohol sensor state machine if(etoh_breathtest(ETOH_DOWORK, loop_msecs ) == ETOH_DONE) { // worker for ETOH switch(etoh_getreward()) { case REWARD_SOBER: tune_startsong(SONG_BUZZER); break; case REWARD_TIPSY: tune_startsong(SONG_PEWPEW); break; case REWARD_DRUNK: tune_startsong(SONG_CACTUS); break; } MyMode = MODE_IDLE; light_show(LIGHTSHOW_SOCFLASH, 5); break; } break; case MODE_ATTEN: // Puts all badges into visual attention mode to see who's in range if(MyMode != lastmode) { attelapsed = 0; } attelapsed += loop_msecs; if(attelapsed > 120000) { // Stays there 2 min (should be in #def) MyMode = MODE_IDLE; } if(MRF49XA_Receive_Packet(rfrxbuf,&rfrxlen) == PACKET_RECEIVED) { MRF49XA_Reset_Radio(); rfcmd_execute(rfrxbuf, rfrxlen); } modelights(); break; case MODE_GETCMD: if (btn_commandwork( loop_msecs) != BTN_WORKING) { MyMode = MODE_IDLE; modelights(); } break; } // switch tune_songwork(); // Worker thread for songs light_animate(loop_msecs); // worker thread for lights if(MyElev) { // Handle the privilege elevation timer if(elapsed_msecs > (elevatemsecs + ELEVATE_DUR)) { MyElev = 0; elevatemsecs = elapsed_msecs + ELEVATE_BASE + (unsigned long) rnd_randomize() * ELEVATE_VAR; modelights(); } } else { if(elapsed_msecs > elevatemsecs) { MyElev = 1; sample_play(); modelights(); } } lastmode = MyMode; } }