k-dot-p 0.1

kdotp::libmodelxx::randomness Namespace Reference

Functions

unsigned long int get_random_unsigned_long_int (GError **err)
unsigned long int get_urandom_unsigned_long_int (GError **err)

Function Documentation

unsigned long int kdotp::libmodelxx::randomness::get_random_unsigned_long_int ( GError **  err)

Definition at line 40 of file randomness.c++.

References RANDOMNESS_GERROR_DOMAIN.

Referenced by get_urandom_unsigned_long_int().

                                                                   {
  #if HOST_OS != LINUX
  #error This currently only supports Linux.
  #else
  long unsigned rnum;
  int fd = open("/dev/random", O_RDONLY);
  if(fd == -1) {
    *err = g_error_new(RANDOMNESS_GERROR_DOMAIN, 1, "libmodelxx::randomness::randomness.c++::get_random_unsinged_long_int: Error while opening /proc/random: %d (%s)\n", errno, strerror(errno));
    return 0;
  }
  ssize_t len = read(fd, &rnum, sizeof(unsigned long int));
  if(len != sizeof(unsigned long int)) {
    *err = g_error_new(RANDOMNESS_GERROR_DOMAIN, 1, "libmodelxx::randomness::randomness.c++::get_random_unsinged_long_int: Failed to read an entire unsigned long int: %d (%s)\n", errno, strerror(errno));
    close(fd);
    return 0;
  }
  close(fd);
  fprintf(stderr, "get_urandom_unsinged_long_int: returning %lu\n", rnum);
  return rnum;
  #endif
      }

Here is the caller graph for this function:

unsigned long int kdotp::libmodelxx::randomness::get_urandom_unsigned_long_int ( GError **  err)

Will get from /dev/urandom if present. Otherwise, is identical to random.

Definition at line 63 of file randomness.c++.

References get_random_unsigned_long_int(), and RANDOMNESS_GERROR_DOMAIN.

Referenced by kdotp::libmetacalc::optimizer::digital_alloy_optimizer_1d::digital_alloy_optimizer_1d(), and kdotp::libmetacalc::optimizer::digital_alloy_optimizer_1d_mpi::digital_alloy_optimizer_1d_mpi().

                                                                    {
  #if HOST_OS != LINUX
  return get_random_unsigned_long_int(err);
  #else
  long unsigned rnum;
  int fd = open("/dev/urandom", O_RDONLY);
  if(fd == -1) {
    *err = g_error_new(RANDOMNESS_GERROR_DOMAIN, 1, "libmodelxx::randomness::randomness.c++::get_urandom_unsinged_long_int: Error while opening /proc/urandom: %d (%s)\n", errno, strerror(errno));
    return 0;
  }
  ssize_t len = read(fd, &rnum, sizeof(unsigned long int));
  if(len != sizeof(unsigned long int)) {
    *err = g_error_new(RANDOMNESS_GERROR_DOMAIN, 1, "libmodelxx::randomness::randomness.c++::get_urandom_unsinged_long_int: Failed to read an entire unsigned long int: %d (%s)\n", errno, strerror(errno));
    close(fd);
    return 0;
  }
  close(fd);
  fprintf(stderr, "get_urandom_unsinged_long_int: returning %lu\n", rnum);
  return rnum;
        #endif
      }

Here is the call graph for this function:

Here is the caller graph for this function:

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines