Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created February 26, 2019 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/ad5ec72c93837547b330556afb7e395d to your computer and use it in GitHub Desktop.
Save parzibyte/ad5ec72c93837547b330556afb7e395d to your computer and use it in GitHub Desktop.
#include <stdio.h> // printf
#include <stdlib.h> // rand y RAND_MAX
#include <unistd.h> // getpid
// Prototipo de funciones
int aleatorio_en_rango(int minimo, int maximo);
// Función main
int main(){
// Hay que alimentar a rand, solamente una vez (seed rand)
srand(getpid());
// Ahora ya podemos llamar a la función
printf("Entre 2, 50 encontramos a %d\n", aleatorio_en_rango(2, 50));
printf("Entre 1, 53 encontramos a %d\n", aleatorio_en_rango(1, 53));
printf("Entre 52, 800 encontramos a %d\n", aleatorio_en_rango(52, 800));
printf("Entre 1, 3 encontramos a %d\n", aleatorio_en_rango(1, 3));
}
// Definir función cuyo prototipo está al inicio
// Devuelve un número aleatorio entre minimo y maximo, incluyendo a minimo y maximo
int aleatorio_en_rango(int minimo, int maximo){
return minimo + rand() / (RAND_MAX / (maximo - minimo + 1) + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment