Plusieurs solutions.
Tu sais que les caractères sont en fait des nombres, en C. Les caractères intéressant a afficher ont des valeurs allant de 40 à 176.
Donc tu génère un rand compris entre 40 et 176, que tu convertis en char.
Ex:
#include <stdio.h>
/*!
** Get a random number between from and to
**
** @param from Range begin
** @param to Range end
**
** @return A random number
*/
int rangeRand(int from, int to)
{
return rand () % (to - from + 1) + from;
}
int main(void)
{
char c[5] = {0}; /* Une case en plus pour le 0 terminal */
int i = 0;
srand(time(0));
for (i = 0; i < 4; ++i)
c[i] = rangeRand(40, 176);
printf("%s\n", c);
return 0;
}
Ou alors, tu génères un tableau de caractères dans lequel tu mets tout ce qu'il te plait, et tu génères la position:
#include <stdio.h>
#include <string.h>
const static char tab[] = "abcdefghiklmnopqrstuvxyz0123456789";
/*!
** Get a random number between from and to
**
** @param from Range begin
** @param to Range end
**
** @return A random number
*/
int rangeRand(int from, int to)
{
return rand () % (to - from + 1) + from;
}
int main(void)
{
char c[5] = {0};
int i = 0;
int size = strlen(tab);
srand(time(0));
for (i = 0; i < 4; ++i)
c[i] = tab[rangeRand(0, size - 1)];
printf("%s\n", c);
return 0;
}