Advertisement
campos20

Untitled

May 20th, 2020
1,631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. // Gera senha aleatoria
  6. // Autor Alexandre Campos
  7.  
  8. // Gera 1 numero aleatorio ate um limite
  9. int numero_aleatorio(int maximo)
  10. {
  11.     return random() % maximo;
  12. }
  13.  
  14. char gera_char_aleatorio()
  15. {
  16.    // Retorna aleatorio entre a e a+26 (sao 26 letras), ou seja, de a ate z
  17.     return 'a' + numero_aleatorio(26);
  18. }
  19.  
  20. char * gera_senha(int tamanho){
  21.     // Aloca o espaco
  22.     char * resultado = malloc(tamanho * sizeof(char));
  23.  
  24.     // Preencher a senha com caracteres aleatorios
  25.     for (int i=0; i<tamanho; i++){
  26.         resultado[i] = gera_char_aleatorio();
  27.     }
  28.  
  29.     return resultado;
  30. }
  31.  
  32. int main()
  33. {
  34.     // Chamar 1 vez
  35.     srand(time(NULL));
  36.  
  37.     // Declaracao
  38.     int tamanho_da_senha = 10;
  39.     char * senha = gera_senha(tamanho_da_senha);
  40.  
  41.     // Exibe a senha
  42.     printf("%s", senha);
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement