Support Forums
Generate Random Number + Characters. - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Programming with C++ (https://www.supportforums.net/forumdisplay.php?fid=20)
+---- Thread: Generate Random Number + Characters. (/showthread.php?tid=10994)



Generate Random Number + Characters. - wchar_t - 08-06-2010

Code:
char *GenerateRandomLetters(unsigned int len)
{
    char            *nick;
    unsigned int    i;

    if (len == 0 || len > MAX_RANDOM_LETTERS)
        len = rand()%(MAX_RANDOM_LETTERS-3) + 3;

    nick = (char *) malloc (len + 1);

    for (i = 0; i <= len; i++)
        nick[i] = (rand()%26) + 97;

    nick[len] = 0;

    return nick;
}

Code:
char *GenerateNumber(int Len)
{
    char *nick;
    int i;
    nick = (char *) malloc (Len);
    nick[0] = '\0';
    srand(GetTickCount());
    for (i = 0; i < Len; i++) {
        sprintf(nick, "%s%d", nick, rand()%10);
    }
    nick[i] = '\0';
    return nick;
}

Maybe usefull for some applications.

Oui