예제 #1
0
파일: rng.c 프로젝트: pyk/simple-nn
/* allocate_rng: Allocate new random number generator from distribution
 * dist to the heap.
 *
 * Possible dist value are:
 * RNG_UNIFORM for uniform distribution
 * RNG_NORMAL or RNG_GAUSSIAN for normal(gaussian) distribution
 *
 * It returns NULL if the malloc(3) fails and set the errno to ENOMEM.
 * It returns pointer to new allocated rng_t if success. */
rng_t *allocate_rng(rng_dist_t dist)
{
    rng_t *rng = malloc(sizeof *rng);
    if(rng == NULL) {
        errno = ENOMEM;
        return NULL;
    }

    /* allocate pcg random number generator to the heap */
    pcg32_random_t *pcg_rng = malloc(sizeof *pcg_rng);
    if(pcg_rng == NULL) {
        errno = ENOMEM;
        return NULL;
    }

    /* initialize the pcg random number generator */
    uint64_t initial_state = time(NULL);
    intptr_t seed_value = (intptr_t)&rng;
    pcg32_srandom_r(pcg_rng, initial_state, seed_value);

    rng->pcg_rng = pcg_rng;
    rng->pcg_initial_state = initial_state;
    rng->pcg_seed_value = seed_value;
    rng->distribution = dist;

    return rng;
}
예제 #2
0
파일: isx.c 프로젝트: habanero-rice/hclib
/*
 * Seeds each rank based on the rank number and time
 */
static pcg32_random_t seed_my_rank(void)
{
  const unsigned int my_rank = shmem_my_pe();
  pcg32_random_t rng;
  pcg32_srandom_r(&rng, (uint64_t) my_rank, (uint64_t) my_rank );
  return rng;
}
예제 #3
0
파일: darytest.c 프로젝트: eiweil/124pset3
int main() {

    Node* nodes = calloc(N, sizeof(Node));

    struct Heap heap;
    heap_init(&heap, nodes, N, D);

    pcg32_random_t rng1;
    pcg32_srandom_r(&rng1, time(NULL), (intptr_t)&rng1);

    for (int i = 0; i < N; i++) {
        Node* newnode = heap.nodes + i;
        newnode->id = i + 1;
        newnode->min_edge = 100 * (float) pcg32_random_r(&rng1) / UINT32_MAX;
    }

    heap_number(&heap);

    printf("unordered: \n");
    print_heap(&heap);

    min_heapify(&heap);

    printf("ordered: \n");
    print_heap(&heap);

    printf("min: %f\n\n", heap_find_min(&heap).min_edge);

    heap_deletemin(&heap);

    printf("deletemin: \n");
    print_heap(&heap);

    Node n = {.min_edge = 2.0, .id = heap.n + 1};

    heap_insert(&heap, n);

    printf("\ninsert: \n");
    print_heap(&heap);

    /*for (int i = 0; i < N; i++) {
        printf("min: %f\n\n", heap_find_min(&heap).min_edge);
        heap_deletemin(&heap);
        printHeap(&heap);
    }
*/
    free_heap(&heap);

}
예제 #4
0
void presentencrypt(char *str, uint8_t *retstr)
{

    pcg32_random_t rng;
    pcg32_srandom_r(&rng, time(NULL) ^ (intptr_t)&printf, (intptr_t)&rounds);
    uint32_t rang = pcg32_random_r(&rng);
    memcpy(iv,&rang,sizeof(iv));

    uint8_t newMessage[200];
    //pad the message string and turn into bytearray
    padStr(str,newMessage);

    cbc_encrypt((uint8_t*)theKey,iv,newMessage,lenmsg);

    memcpy(retstr,newMessage,sizeof(newMessage));

    printf("sizeof(newMessage) %d\n", sizeof(newMessage));
}  
예제 #5
0
void pcg32_srandom(uint64_t seed, uint64_t seq)
{
    pcg32_srandom_r(&pcg32_global, seed, seq);
}
예제 #6
0
파일: myssl.c 프로젝트: captdeaf/pennmush
/** Initialize the SSL context.
 * \return pointer to SSL context object.
 */
SSL_CTX *
ssl_init(char *private_key_file, char *ca_file, char *ca_dir,
         int req_client_cert)
{
  const SSL_METHOD
    *meth; /* If this const gives you a warning, you're
              using an old version of OpenSSL. Walker, this means you! */
  /* uint8_t context[128]; */
  unsigned int reps = 1;
  pcg32_random_t rand_state;
  uint64_t seeds[2];
  bool seeded = false;

  if (!bio_err) {
    if (!SSL_library_init())
      return NULL;
    SSL_load_error_strings();
    /* Error write context */
    bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  }

  lock_file(stderr);
  fputs("Seeding OpenSSL random number pool.\n", stderr);
  unlock_file(stderr);
  while (!RAND_status()) {
    /* At this point, a system with /dev/urandom or a EGD file in the usual
       places will have enough entropy. Otherwise, be lazy and use random
       numbers until it's satisfied. */
    uint32_t gibberish[8];
    int n;

    if (!seeded) {
      generate_seed(seeds);
      pcg32_srandom_r(&rand_state, seeds[0], seeds[1]);
      seeded = 1;
    }

    for (n = 0; n < 8; n++)
      gibberish[n] = pcg32_random_r(&rand_state);

    RAND_seed(gibberish, sizeof gibberish);

    reps += 1;
  }

  lock_file(stderr);
  fprintf(stderr, "Seeded after %u %s.\n", reps, reps > 1 ? "cycles" : "cycle");
  unlock_file(stderr);

  /* Set up SIGPIPE handler here? */

  /* Create context */
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  meth = TLS_server_method();
#else
  meth = SSLv23_server_method();
#endif
  ctx = SSL_CTX_new(meth);
  SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);

  /* Load keys/certs */
  if (private_key_file && *private_key_file) {
    if (!SSL_CTX_use_certificate_chain_file(ctx, private_key_file)) {
      ssl_errordump("Unable to load server certificate - only anonymous "
                    "ciphers supported.");
    }
    if (!SSL_CTX_use_PrivateKey_file(ctx, private_key_file, SSL_FILETYPE_PEM)) {
      ssl_errordump(
        "Unable to load private key - only anonymous ciphers supported.");
    }
  }

  /* Load trusted CAs */
  if ((ca_file && *ca_file) || (ca_dir && *ca_dir)) {
    if (!SSL_CTX_load_verify_locations(ctx,
                                       (ca_file && *ca_file) ? ca_file : NULL,
                                       (ca_dir && *ca_dir) ? ca_dir : NULL)) {
      ssl_errordump("Unable to load CA certificates");
    }
    {
      STACK_OF(X509_NAME) *certs = NULL;
      if (ca_file && *ca_file)
        certs = SSL_load_client_CA_file(ca_file);
      if (certs)
        SSL_CTX_set_client_CA_list(ctx, certs);

      if (req_client_cert)
        SSL_CTX_set_verify(ctx,
                           SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
                           client_verify_callback);
      else
        SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, client_verify_callback);
#if (OPENSSL_VERSION_NUMBER < 0x0090600fL)
      SSL_CTX_set_verify_depth(ctx, 1);
#endif
    }
  }

  SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE | SSL_OP_ALL);
  SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE |
                          SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);

  /* Set up DH key */
  {
    DH *dh;
    dh = get_dh2048();
    SSL_CTX_set_tmp_dh(ctx, dh);
    DH_free(dh);
  }

#ifdef NID_X9_62_prime256v1
  /* Set up ECDH key */
  {
    EC_KEY *ecdh = NULL;
    ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
    SSL_CTX_set_tmp_ecdh(ctx, ecdh);
    EC_KEY_free(ecdh);
  }
#endif

  /* Set the cipher list to the usual default list, except that
   * we'll allow anonymous diffie-hellman, too.
   */
  SSL_CTX_set_cipher_list(ctx, "ALL:ECDH:ADH:!LOW:!MEDIUM:@STRENGTH");

  /* Set up session cache if we can */
  /*
     strncpy((char *) context, MUDNAME, 128);
     SSL_CTX_set_session_id_context(ctx, context, strlen(context));
   */

  return ctx;
}
예제 #7
0
void kmeans(double *X, int N, int p, int metric, int *clusterids, int K, int init, double tol, int nruns) {
    /*
    kmeans algorithm, currently featuring:
      +choice of distance metric (careful when not using euclidean!)
      +initialization with random centers (init = 0) or kmeans++ (init = 1)
      +returns best clustering result from nruns tries
      +printf for errors instead of error code return.
    function returns void but stores the best cluster assignments in clusterids
    */

    double centerchg,odist,ndist,ssqd;

    /* number of exemplars of each class */
    double *nSamples = malloc(K*sizeof(double));
    /* indices of starting centers (rows of X) */
    int *centerloc = malloc(K*sizeof(int));
    /* cluster centers */
    double *oldCenters = malloc(K*p*sizeof(double));
    double *newCenters = malloc(K*p*sizeof(double));
    /* temporary labels */
    int *labels = malloc(N*sizeof(int));

    // RNG stuff
    pcg32_random_t rng;
    pcg32_srandom_r(&rng, time(NULL), (intptr_t)&rng);

    double minssqd = INFINITY;

    // distance selection
    double (*metricptr)(double *,double *,int);
    if (metric == 0) {
      // euclidean distance
      metricptr = &euclidean;
    } else if (metric == 1) {
      // cosine distance
      metricptr = &cosine;
    } else if (metric == 2) {
      // correlation distance
      metricptr = &correlation;
    } else {
      // default to manhattan
      metricptr = &manhattan;
    }


    for (int irun = 0; irun < nruns; irun++) {
        centerchg = 1.0+tol;

        /* initialize cluster centers */
        if (init == 0) {
          /* random centers */
          choice_floyd(&rng,centerloc,K,N);
        } else if (init == 1){
          /* kmeans++ */
          /* holds pdf and cdf for point selection */
          double *pdf = malloc(N*sizeof(double));
          double *cdf = malloc(N*sizeof(double));
          /* first center is chosen uniformly from the data (just ignore all
            elements but centerloc[0] when we proceed) */
          choice_floyd(&rng,centerloc,K,N);
          /* now choose remaining centers */
          for (int k = 1; k < K; k++){
            /* compute center distances */
            double sumDxsq = 0.0;
            for (int d = 0; d < N; d ++) {
                /* distance will be the minimum distance from the point to
                  the closest center yet found */
              double mindist = INFINITY;
              for (int j = 0; j < k; j++){
                mindist = fmin(mindist,metricptr(&X[d*p],&X[centerloc[j]*p],p));
              }
              pdf[d] = mindist*mindist;
              sumDxsq += pdf[d];
            }
            /* compute cdf */
            cdf[0] = pdf[0]/sumDxsq;
            for (int d = 1; d < N; d++) {
              cdf[d] = pdf[d]/sumDxsq + cdf[d-1];
            }
            /* now we have the probability vector; find the new center */
            centerloc[k] = rand_cdf_index(&rng,cdf,N);
          }
          /* have the centers, free memory */
          free(pdf);
          free(cdf);
        } else {
          printf("ERROR! Unknown initialization method.");
        }


        for (int i = 0; i < K; i++) {
            for (int j = 0; j < p; j++) {
                oldCenters[i*p + j] = X[centerloc[i]*p + j];
            }
        }

        /* run until convergence */
        for (int r = 0; r < 1000; r++) {

            /* find closest center */
            for (int i = 0; i < N; i++) {
                odist = metricptr(&X[i*p],&oldCenters[0],p);
                labels[i] = 0;
                for (int k = 0; k < K; k++) {
                    ndist = metricptr(&X[i*p],&oldCenters[k*p],p);
                    if (ndist < odist) {
                        odist = ndist;
                        labels[i] = k;
                    }
                }
            }

            /* recompute new centers */
            for (int k = 0; k < K; k++) {
                nSamples[k] = 0.0;
                for(int j = 0; j < p; j++) {
                    newCenters[k*p + j] = 0.0;
                }
            }
            for (int i = 0; i < N; i++) {
                nSamples[labels[i]] += 1.0;
            }

            for (int j = 0; j < N ; j++) {
                for (int l = 0; l < p; l++) {
                    newCenters[labels[j]*p + l] += X[j*p + l]/nSamples[labels[j]];
                }
            }

            /* check to see if centers have changed enough to continue */
            centerchg = 0.0;
            for (int k = 0; k < K; k++) {
                centerchg += metricptr(&newCenters[k*p],&oldCenters[k*p],p);
            }
            if (centerchg < tol) {
                break;
            }

            /* copy newCenters into oldCenters */
            for (int i = 0; i < K; i++) {
                for (int j = 0; j < p; j++) {
                    oldCenters[i*p + j] = newCenters[i*p + j];
                }
            }

        }
    /* compute ssd and copy into clusterids in case this is the best run */
        ssqd = 0.0;
        for (int i = 0; i < N; i++) {
            ssqd += metricptr(&X[i*p],&newCenters[labels[i]*p],p);
        }
        if (ssqd < minssqd) {
            minssqd = ssqd;
            memcpy(clusterids,labels,N*sizeof(*clusterids));
        }
    }


    /* free memory */
    free(nSamples);
    free(labels);
    free(centerloc);
    free(oldCenters);
    free(newCenters);

    return;
}
예제 #8
0
int main(int argc, char** argv)
{
    // Read command-line options

    int rounds = 5;
    bool nondeterministic_seed = false;
    int round, i;

    ++argv;
    --argc;
    if (argc > 0 && strcmp(argv[0], "-r") == 0) {
        nondeterministic_seed = true;
        ++argv;
        --argc;
    }
    if (argc > 0) {
        rounds = atoi(argv[0]);
    }

    // In this version of the code, we'll use a local rng, rather than the
    // global one.

    pcg32_random_t rng;

    // You should *always* seed the RNG.  The usual time to do it is the
    // point in time when you create RNG (typically at the beginning of the
    // program).
    //
    // pcg32_srandom_r takes two 64-bit constants (the initial state, and the
    // rng sequence selector; rngs with different sequence selectors will
    // *never* have random sequences that coincide, at all) - the code below
    // shows three possible ways to do so.

    if (nondeterministic_seed) {
        // Seed with external entropy -- the time and some program addresses
        // (which will actually be somewhat random on most modern systems).
        // A better solution, entropy_getbytes, using /dev/random, is provided
        // in the full library.
        
        pcg32_srandom_r(&rng, time(NULL) ^ (intptr_t)&printf, 
			      (intptr_t)&rounds);
    } else {
        // Seed with a fixed constant

        pcg32_srandom_r(&rng, 42u, 54u);
    }

    printf("pcg32_random_r:\n"
           "      -  result:      32-bit unsigned int (uint32_t)\n"
           "      -  period:      2^64   (* 2^63 streams)\n"
           "      -  state type:  pcg32_random_t (%zu bytes)\n"
           "      -  output func: XSH-RR\n"
           "\n",
           sizeof(pcg32_random_t));

    for (round = 1; round <= rounds; ++round) {
        printf("Round %d:\n", round);
        /* Make some 32-bit numbers */
        printf("  32bit:");
        for (i = 0; i < 6; ++i)
            printf(" 0x%08x", pcg32_random_r(&rng));
        printf("\n");

        /* Toss some coins */
        printf("  Coins: ");
        for (i = 0; i < 65; ++i)
            printf("%c", pcg32_boundedrand_r(&rng, 2) ? 'H' : 'T');
        printf("\n");

        /* Roll some dice */
        printf("  Rolls:");
        for (i = 0; i < 33; ++i) {
            printf(" %d", (int)pcg32_boundedrand_r(&rng, 6) + 1);
        }
        printf("\n");

        /* Deal some cards */
        enum { SUITS = 4, NUMBERS = 13, CARDS = 52 };
        char cards[CARDS];

        for (i = 0; i < CARDS; ++i)
            cards[i] = i;

        for (i = CARDS; i > 1; --i) {
            int chosen = pcg32_boundedrand_r(&rng, i);
            char card = cards[chosen];
            cards[chosen] = cards[i - 1];
            cards[i - 1] = card;
        }

        printf("  Cards:");
        static const char number[] = {'A', '2', '3', '4', '5', '6', '7',
                                      '8', '9', 'T', 'J', 'Q', 'K'};
        static const char suit[] = {'h', 'c', 'd', 's'};
        for (i = 0; i < CARDS; ++i) {
            printf(" %c%c", number[cards[i] / SUITS], suit[cards[i] % SUITS]);
            if ((i + 1) % 22 == 0)
                printf("\n\t");
        }
        printf("\n");

        printf("\n");
    }

    return 0;
}
예제 #9
0
/**
 * The E-means algorithm, uses a genetic algorithm to optimize the parameters 
 * for the K-means implemetation of clustering based Lloyds clustering algorithm.
 *
 * @return        Status code, 0 for SUCCESS, 1 for ERROR
 */
int emeans(void)
{
    gsl_matrix *data = NULL,
               *bounds = NULL,
               *parent1 = NULL,
               *parent2 = NULL,
               **population = NULL,
               **new_population = NULL,
               ***clusters = NULL;
    int status = SUCCESS;
    double fitness[size],
           probability[size];

    // Initialize the PRNG
    pcg32_random_t rng;
    int rounds = 5;
    pcg32_srandom_r(&rng, time(NULL) ^ (intptr_t)&printf, (intptr_t)&rounds);

    // Allocate memory and load the data
    data = gsl_matrix_alloc(data_rows, data_cols);
    bounds = gsl_matrix_alloc(data_cols, 2);
    parent1 = gsl_matrix_alloc(n_clusters, data_cols);
    parent2 = gsl_matrix_alloc(n_clusters, data_cols);
    population = (gsl_matrix **)calloc(size, sizeof(gsl_matrix **));
    new_population = (gsl_matrix **)calloc(size, sizeof(gsl_matrix **));
    clusters = (gsl_matrix ***)calloc(size, sizeof(gsl_matrix ***));

    for (int i = 0; i < (int)size; ++i)
    {
        clusters[i] = (gsl_matrix **)calloc(n_clusters, sizeof(gsl_matrix **));
        population[i] = gsl_matrix_alloc(n_clusters, data_cols);
        new_population[i] = gsl_matrix_alloc(n_clusters, data_cols);
    }
    if ((status = load_data(data_file, data)) != SUCCESS)
    {   
        fprintf(stderr, RED "Unable to load data!\n" RESET);
        status = ERROR;
        goto free;
    }

    // Calculate the bounds of the data
    calc_bounds(data, bounds);

    // Generate the initial population
    printf(CYAN "Generating initial population...\n" RESET);
    for (int i = 0; i < (int)size; ++i)
    {
        random_centroids(population[i], bounds, &rng);
    }

    // Perform the Genetic Algorithm
    for (int iter = 0; iter < max_iter; ++iter)
    {
        // Compute the fitness of each chromosome
        for (int i = 0; i < (int)size; ++i)
        {
            lloyd_defined(trials, population[i], data, n_clusters, clusters[i]);
            fitness[i] = dunn_index(population[i], n_clusters, clusters[i]);
            if (VERBOSE == 1)
                printf(CYAN "chromsome[%d], fitness: %10.6f\n" RESET, i, fitness[i]);
        }

        // Generate the probabilities for roulette wheel selection
        gen_probability(size, fitness, probability);

        // Save the results if there is a new best solution
        save_results(fitness_file, centroids_file, cluster_file, size, fitness, 
                     population, n_clusters, clusters);

        // Perform roulette wheel selection and GA operators
        if (VERBOSE == 1)
            printf(CYAN "Executing roulette wheel selection...\n" RESET);

        // Select parents from the population and perform genetic operators
        for (int i = 0; i < size; i += 2)
        {
            // Select parents
            for (int j = 0, idx = 0; j < 2; ++j)
            {
                idx = select_parent(size, probability, &rng);
                if (VERBOSE == 1)
                    printf(CYAN "Parent %d selected as chromsome %d from population\n" RESET, j, idx);

                if (j == 0)
                    gsl_matrix_memcpy(parent1, population[idx]);
                else
                    gsl_matrix_memcpy(parent2, population[idx]);
            }

            // Perform crossover and mutation with specified probabilities
            if (VERBOSE == 1)
                printf(CYAN "Performing crossover...\n" RESET);
            if (pcg32_random_r(&rng) / (double)UINT32_MAX <= c_rate)
            {
                crossover(parent1, parent2, &rng);
            }

            if (VERBOSE == 1)
                printf(CYAN "Performing background mutation...\n" RESET);
            for (int j = 0; j < 2; ++j)
            {
                if (pcg32_random_r(&rng) / (double)UINT32_MAX <= m_rate)
                {
                    if (j == 0)
                        mutate(parent1, bounds, &rng);
                    else
                        mutate(parent2, bounds, &rng);
                }
            }

            // Copy each parent to the new population
            if (VERBOSE == 1)
                printf(CYAN "Copying parents to new population...\n" RESET);
            gsl_matrix_memcpy(new_population[i], parent1);
            gsl_matrix_memcpy(new_population[i+1], parent2);
        }

        // Copy the new population to the next population
        if (VERBOSE == 1)
            printf(CYAN "Copying current population as new population\n" RESET);

        for (int i = 0; i < size; ++i)
        {
            gsl_matrix_memcpy(population[i], new_population[i]);
        }

        // Check if stop signal, terminate if present
        if (access("./stop", F_OK) != -1)
        {
            printf(YELLOW "Stop signal received, shutting down!\n" RESET);
            remove("./stop");
            break;
        }

    }
    printf(GREEN "Finished executing E-means, shutting down!\n" RESET);

free:
    for (int i = 0; i < (int)size; ++i)
    {
        for (int j = 0; j < n_clusters; ++j)
        {
            gsl_matrix_free(clusters[i][j]);
        }
        free(clusters[i]);
    }
    free(clusters);
    for (int i = 0; i < (int)size; ++i)
    {
        gsl_matrix_free(population[i]);
        gsl_matrix_free(new_population[i]);
    }
    free(population);
    free(new_population);
    gsl_matrix_free(data);
    gsl_matrix_free(bounds);
    return status;
}
예제 #10
0
void set_seed(uint64_t initstate, uint64_t initseq){
    pcg32_srandom_r(&rng, initstate, initseq);
}