Search code examples
crandom

How to randomly select 6-letter words from a 26-letter alphabet


I would like to randomly select words (with or without meaning) from 26 letters. The word contains 6 letters, and letter repetitions are allowed.

How can this be done with a program in C (or Objective C)? I would be very grateful for any ideas.


Solution

  • #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    main()
    {
        srand(time(NULL));
        for (int i = 0; i < 6; ++i)
        {
            putchar('a' + (rand() % 26));
        }
        putchar('\n');
        return EXIT_SUCCESS;
    }
    

    Salt to taste.

    Update0

    It would seem the OP does not know how to compile the above. If saved into a file called so.c, compile it with make so CFLAGS=-std=c99 from the same folder.