-3

If I want to create a username generator in C? How do I go about it? I do understand that the question has been asked. But, I don't see anyone asking it in C language. I think making it from a random word from the dictionary is going to be a little complicated for me to understand. I am right now only going to concentrate on a simple thing, and that is GENERATING 4 RANDOM NUMBERS. Now, before anyone says it's not secure or anything, this is purely for my curiosity and tinkering experience! I am enjoying learning C and I want to learn more.

The code that I have been able to come up with (minus the generator!) is as follows:

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

int main()
{

int randomnumbers; // this is for storing whatever random numbers I may   generate
char NAME[15];
prinf("What is your first name? \n");// 
scanf("%s \n", NAME);// I put it caps so that I can easily see it

printf("The username for %s is %s%d \n",NAME,NAME,randomnumbers);

return 0;
}

In case you find that the code is lacking and is of poor taste, I apologise. I am a newbie with less than a week in experience in coding. I thank you for your help and guidance.

returnNULL
  • 19
  • 6

1 Answers1

1

rand is what you need.

int randomnumbers = rand(); 

You may also want to read on how to seed the random number generator

Bonus random username generator:

// buffer must have memory for len + 1 elements
void generateUserName(/*out*/ char *buffer, int len)
{
   srand(time(NULL));
   int idx = 0;
   while (idx < len) buffer[idx++] = rand() % 26 + 'a';
   buffer[len] = '\0';
}

To be called as

char userName[10];
generateUserName(userName, sizeof(userName) - 1);
printf("%s\n", userName);
bashrc
  • 4,725
  • 1
  • 22
  • 49