"Código de Ejemplo para la Generación de Números Aleatorios"



USO DE LA FUNCIÓN srand() Y rand() PARA LA GENERACIÓN PSEUDOALEATORIA

Código:
/*
  Archivo: ArchivosEscribir.c
  Descripcion: Este programa permite el manejo de archivos para escribir
               informacion con la funcion printf()
  Autor: Basurto Páez Gustavo
  Fecha: 03 de julio de 2008
*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define MAX 20

void GeneradorAleatorioTiempo();
void GeneradorPseudoAleatorio();

int main()
{
  GeneradorPseudoAleatorio();
  GeneradorAleatorioTiempo();
  getch();
  return (0);
}

void GeneradorPseudoAleatorio()
{
  int cont;
 
  //Funcion que inicializa la generacion aleatoria para las llamadas de
  //la funcion rand.... el argumento es una 'semilla' que si se varía 
  //generan nuevos valores aleatorios
  srand( 1000 );
 
  printf("\n\n\t\"GENERADOR DE NUMEROS PSEUDOALEATORIOS\"\n");

  for(cont=0; cont<MAX; cont++)
  {
           printf(" \t\t %d \n", rand());
           //Si se requiera un limite utilice el operador modulo
           //printf(" \t\t %d \n", rand() % 50 ); //Genera entre 0 y 49
           //printf(" \t\t %d n", rand() % 50 +1  ); //Genera entre 1 y 50
  }
  printf("\n\n");
}

void   GeneradorAleatorioTiempo()
{
  int cont;
  time_t t;  //estructura para la obtener el tiempo real del sistema.

 

  //Funcion que inicializa la generacion aleatoria para las llamadas de
  //la funcion rand.... el argumento es una 'semilla' que se varía 
  //pasandole como semilla el tiempo real del sistema
  //lo que permite obtener numeros aleatorios
  srand( time(NULL) );
 // srand((unsigned) time(&t));  
  printf("\n\n\t\"GENERADOR DE NUMEROS ALEATORIOS\"\n");

  for(cont=0; cont<MAX; cont++)
  {
           printf(" \t\t %d \n", rand());
  }
  printf("\n\n");
     
}

Evidencia de Ejecución 1

        "GENERADOR DE NUMEROS PSEUDOALEATORIOS"
                 3304
                 8221
                 26849
                 14038
                 1509
                 6367
                 7856
                 21362
                 6968
                 10160
                 21787
                 6611
                 21618
                 31535
                 28764
                 19740
                 22714
                 3125
                 31476
                 11926

 

 

        "GENERADOR DE NUMEROS ALEATORIOS"
                 12384
                 28085
                 3175
                 19054
                 10105
                 23207
                 29469
                 8423
                 19063
                 11183
                 19012
                 21500
                 16860
                 25813
                 19727
                 7202
                 26134
                 14178
                 30710
                 4864

Evidencia de Ejecución 2

        "GENERADOR DE NUMEROS PSEUDOALEATORIOS"
                 3304
                 8221
                 26849
                 14038
                 1509
                 6367
                 7856
                 21362
                 6968
                 10160
                 21787
                 6611
                 21618
                 31535
                 28764
                 19740
                 22714
                 3125
                 31476
                 11926

 

 

        "GENERADOR DE NUMEROS ALEATORIOS"
                 12534
                 30992
                 5727
                 11854
                 25823
                 7229
                 8802
                 4841
                 24932
                 23283
                 12208
                 23526
                 19440
                 512
                 24758
                 18322
                 14578
                 9770
                 16619
                 5655

 

Hosted by www.Geocities.ws

1