generar numero aleatorio en c code example

Example: random en c

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

//con vectores
void random(int tam){
  int v[tam];
  srand(time(NULL));

  for(int i = 0; i <= tam; i++){
    v[i] = rand() % 10;
    //v[i] = rand()%5; //Con el 5, son numeros desde 0 a 5
    printf("v[%d] = %d\n", i, v[i]);
  }
}

//Con matrices
#define kFIL 3
#define kCOL 5

typedef int TMatriz [kFIL][kCOL];

void random(TMatriz m){
    int i, j;
    srand(time(NULL));

    for( i = 0; i < kFIL; i++){
      for(j = 0; j < kCOL; j++){
        m[i][j] = rand() % 100;
      }
    }
}

Tags:

Cpp Example