-
-
Save parzibyte/ad5ec72c93837547b330556afb7e395d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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