Thread: Generating random letters

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    66

    Generating random letters

    im writing a program for practice that writes 1000 random words to an output file. the random words dont have to actual words just a bunch of lower case letters strung together in random lengths of 3 - 9 letters.

    im guessing that i would need to generate random numbers 0-25 and then add 65 to correlate them to ascii. I dont know what function to use to generate random numbers between 0 and 25 and i cant really figure out how to add them together in random lengths of 3 - 9 letters.

    here is my attempt:

    Code:
    void rand_words() {
        int     i = 0, j = 0; 
        int     rand_num = 0;
        int     n;
        char    word[10];
    
    
            for( i=0; i<=1000; i++) {
                n = random_num_gen(0-25); // not a clue...
                n += 65;
                // need a line to convert numbers to letters
                rand_num = random_num_gen(3-9); // again, no idea what this function needs to be
                
                for( j=0; j<=rand_num; j++) {
                    word += n;
                }
                    
                    
                    
                fprintf("%s\n", word)
            }
            
    }


    i havent even tried to compile this but im pretty sure i got most of what i need

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( x = 0; x < numberofwords; x++ )
    {
        numberofletters = 3 + rand() % 7;
        for( y = 0; y < numberofletters; y++ )
        {
            fputc( 'a' + ( rand( ) % ('z' - 'a') ), here );
        }
        fputc( '\n', here );
    }
    Look at the FAQ on generating random numbers.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    yeah, that didnt really help. i've looked at the FAQ's but i cant find anything on how to generate random #s between two numbers

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ominub View Post
    yeah, that didnt really help.
    Then you're dumb. I basically did the entire thing for you.
    Quote Originally Posted by ominub View Post
    i've looked at the FAQ's but i cant find anything on how to generate random #s between two numbers
    Then you're an idiot, or you're illiterate. Sheesh, do I have to do EVERYTHING for you?

    Now this may be tricky, but that red thing there is a link. You should click on it. Then you should read.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    well, shucks. thank you o' great one. i didnt ask you to write the program. i asked how to generate random numbers.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I showed you how to generate random numbers, and I told you the FAQ covered it as well. It's not my fault you can't read.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ominub View Post
    yeah, that didnt really help. i've looked at the FAQ's but i cant find anything on how to generate random #s between two numbers
    I think quzah went a bit far, he must be having a bad day to break out the personal attacks. I can certainly agree with his bluntness though in that, in addition to not being able to search the FAQ properly, you have a terrible case of googleblindness. This search was the first thing that popped into my head:
    random number between two numbers in C - Google Search
    To make it as a programmer, you have to learn to be able to find things out for yourself.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random 2D mountains/terrain
    By Flaug in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2009, 02:49 PM
  2. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  3. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 01:00 PM
  4. Replies: 7
    Last Post: 09-26-2005, 05:09 PM
  5. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 02:16 PM