Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created February 27, 2019 23:59
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/b0f0656ac977a12de5527b128d61b78d to your computer and use it in GitHub Desktop.
Save parzibyte/b0f0656ac977a12de5527b128d61b78d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// Devuelve un número aleatorio en un rango
int aleatorio_en_rango(int minimo, int maximo) {
return minimo + rand() / (RAND_MAX / (maximo - minimo + 1) + 1);
}
int main(void) {
// seed rand
srand(getpid());
int arreglo[] = {57, 84, 62, 486, 22, 19, 800};
// Si quieres calcularla mira:
// https://parzibyte.me/blog/2018/09/21/longitud-de-un-arreglo-en-c/
int longitudArreglo = 7;
// Obtener un índice en un rango
printf("Elemento aleatorio: %d\n",
arreglo[aleatorio_en_rango(0, longitudArreglo - 1)]);
printf("Elemento aleatorio: %d\n",
arreglo[aleatorio_en_rango(0, longitudArreglo - 1)]);
printf("Elemento aleatorio: %d\n",
arreglo[aleatorio_en_rango(0, longitudArreglo - 1)]);
printf("Elemento aleatorio: %d\n",
arreglo[aleatorio_en_rango(0, longitudArreglo - 1)]);
printf("Elemento aleatorio: %d\n",
arreglo[aleatorio_en_rango(0, longitudArreglo - 1)]);
printf("Elemento aleatorio: %d\n",
arreglo[aleatorio_en_rango(0, longitudArreglo - 1)]);
printf("Elemento aleatorio: %d\n",
arreglo[aleatorio_en_rango(0, longitudArreglo - 1)]);
printf("Elemento aleatorio: %d\n",
arreglo[aleatorio_en_rango(0, longitudArreglo - 1)]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment