示例#1
0
int main (int argc, char *argv[])
{
  FILE *infile;
  char word[1000];
  long seed;
  int i, j;

  if (argc != 2) {
    die("USAGE: utils <filename>");
  }

  if (open_file(argv[1], "r", 1, "input", "", &infile) == 0)
    exit(1);

  while (fscanf(infile, "%s", word) == 1)
    printf("%s ", word);

  fclose(infile);

  /* Test the random number generator. */
  seed = time(0);
  my_srand(seed);
  printf("\nSome random numbers (seed=%ld): \n", seed);
  for (i = 0; i < 10; i++) {
    for (j = 0; j < 10; j++) {
      printf("%6.4f ", my_drand());
    }
    printf("\n");
  }
  return(0);
}
示例#2
0
/***********************************************************************
 * Fill an array with random values between 0 and a given maximum.
 *
 * Assumes that the random number generator is initialized. 
 ***********************************************************************/
void randomize_array
  (ATYPE    magnitude,
   ARRAY_T* array)
{
  int num_items;
  int i_item;

  check_null_array(array);

  num_items = get_array_length(array);
  for (i_item = 0; i_item < num_items; i_item++) {
    set_array_item(i_item, my_drand() * magnitude, array);
  }
}
示例#3
0
/***********************************************************************
 * Add random noise to an array.
 ***********************************************************************/
void add_noise
  (ATYPE    magnitude,  /* Magnitude of the noise. */
   ARRAY_T* array)
{
  int   i_item;
  int   num_items;
  ATYPE noise;

  check_null_array(array);  

  num_items = get_array_length(array);
  for (i_item = 0; i_item < num_items; i_item++) {
    noise = magnitude * (2 * my_drand() - 1);
    incr_array_item(i_item, noise, array);
  }
}
void* Thread_work(void* rank)
{
long my_rank = (long) rank;
int i, val;
double which_op;
unsigned seed = my_rank + 1;
int my_member=0, my_insert=0, my_delete=0;
int ops_per_thread = total_ops/thread_count;
for (i = 0; i < ops_per_thread; i++)
   {
   which_op = my_drand(&seed);
   val = my_rand(&seed) % MAX_KEY;
   if (which_op < porcentaje_busqueda)
   {
#     ifdef DEBUG
      printf("Hilo %ld > Buscando %d\n", my_rank, val);
#     endif
      Member(val);
      my_member++;
   }
   else if (which_op < porcentaje_busqueda + porcentaje_insercion)
   {
#     ifdef DEBUG
      printf("Hilo %ld > Insertando %d\n", my_rank, val);
#     endif
      Insert(val);
      my_insert++;
   }
   else 
   {
#     ifdef DEBUG
      printf("Hilo %ld > Borrando %d\n", my_rank, val);
#     endif
      Delete(val);
      my_delete++;
   }
}

   pthread_mutex_lock(&count_mutex);
   total_miembro += my_member;
   total_insertar += my_insert;
   total_borrado += my_delete;
   pthread_mutex_unlock(&count_mutex);

   return NULL;
}
int main(void) {
   int n, i; 
   unsigned seed = 1, x;
   double y;

   printf("Cuantos numeros aleatorios?\n");
   scanf("%d", &n);

   x = my_rand(&seed);
   for (i = 0; i < n; i++) {
      x = my_rand(&x);
      printf("%u\n", x);
   }
   for (i = 0; i < n; i++) {
      y = my_drand(&x);
      printf("%e\n", y);
   }
   return 0;
}
示例#6
0
int main(void) {
   int n, i; 
   unsigned seed = 1, x;
   double y;

   printf("How many random numbers?\n");
   scanf("%d", &n);

   x = my_rand(&seed);
   for (i = 0; i < n; i++) {
      x = my_rand(&x);
      printf("%u\n", x);
   }
   for (i = 0; i < n; i++) {
      y = my_drand(&x);
      printf("%e\n", y);
   }
   return 0;
}
示例#7
0
/*****************************************************************************
 * Shuffle an array of integers using Knuth's algorithm.
 *****************************************************************************/
static void shuffle
  (int   num_items,
   int*  data)
{
  int i_item;
  int i_rand;
  int temp_swap;

  // Shuffle the array.
  for (i_item = 0; i_item < num_items; i_item++) {

    // Select a random position to the right of the current position.
    i_rand = (int)(my_drand() * (double)(num_items - i_item)) + i_item;

    // Swap 'em.
    temp_swap = data[i_item];
    data[i_item] = data[i_rand];
    data[i_rand] = temp_swap;
  }
}
示例#8
0
/***********************************************************************
 * Create a bootstrapped copy of the given array.
 *
 * Allocates the bootstrap array; must be freed by caller.
 * Assumes that the random number generator is initialized. 
 ***********************************************************************/
ARRAY_T* bootstrap_array
  (ARRAY_T* source_array, 
   int      num_items)
{
  ARRAY_T* return_array;

  check_null_array(source_array);

  // Allocate the bootstrap array.
  return_array = allocate_array(num_items);

  // Fill up the bootstrap array.
  int i_item;
  int num_inputs = get_array_length(source_array);
  for (i_item = 0; i_item < num_items; i_item++) {
    int random_index = (int)(my_drand() * (double)(num_inputs));
    ATYPE random_item = get_array_item(random_index, source_array);
    set_array_item(i_item, random_item, return_array);
  }
  return(return_array);
}
示例#9
0
/*-----------------------------------------------------------------*/
void* Thread_work(void* rank) {
   long my_rank = (long) rank;
   int i, val;
   double which_op;
   unsigned seed = my_rank + 1;
   int my_member=0, my_insert=0, my_delete=0;
   int ops_per_thread = total_ops/thread_count;

   for (i = 0; i < ops_per_thread; i++) {
      which_op = my_drand(&seed);
      val = my_rand(&seed) % MAX_KEY;
      if (which_op < search_percent) {
         pthread_mutex_lock(&mutex);
         Member(val);
         pthread_mutex_unlock(&mutex);
         my_member++;
      } else if (which_op < search_percent + insert_percent) {
         pthread_mutex_lock(&mutex);
         Insert(val);
         pthread_mutex_unlock(&mutex);
         my_insert++;
      } else { /* delete */
         pthread_mutex_lock(&mutex);
         Delete(val);
         pthread_mutex_unlock(&mutex);
         my_delete++;
      }
   }  /* for */

   pthread_mutex_lock(&count_mutex);
   member_total += my_member;
   insert_total += my_insert;
   delete_total += my_delete;
   pthread_mutex_unlock(&count_mutex);

   return NULL;
}  /* Thread_work */