mercredi 23 novembre 2016

Generate random letters in C

here is my code. I am trying generate random alphabet but i see same letters. example: (YHTGDHFBSHXCHFYFUXZWDYKLXI) How can i fix it? just i need mixed alphabet not same letters. Thank you so much.

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

void random_string(char * string, unsigned length)
{
    /* Seed number for rand() */
    srand((unsigned int) time(0));

    /* ASCII characters 33 to 126 */
    int i;
    for (i = 0; i < length; ++i)
    {
        string[i] = rand() % 26 + 'A';
    }

    string[i] = '\0';
}

int main(void)
{
    char s[26];
    random_string(s, 26);
    printf("%s\n", s);
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire