Thread: Random letters

  1. #1
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    Random letters

    Ok if I make a program can I have it print jibberish kinda like it just picks them or even better if you can set it up so that it takes the same characters and just reprints them in a different order, thanks a lot.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    This should give you some random letters.

    Code:
    char randChar = ( rand( ) % 26 ) + 'a';
    putc( randChar );
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ok, could you give me an actual example?(more full I mean) I tried it out with:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char randChar = ( rand( ) % 26 ) + 'a';
    putc( randChar );
    
    return 0;
    }
    Now I have never done this before so I wasn't sure how to do it, how far away from getting this right was I?
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  4. #4
    Unregistered
    Guest
    Originally posted by CAP
    Ok, could you give me an actual example?(more full I mean) I tried it out with:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char randChar = ( rand( ) % 26 ) + 'a';
    putc( randChar );
    
    return 0;
    }
    Now I have never done this before so I wasn't sure how to do it, how far away from getting this right was I?
    There isn't really much to it. You do need to seed the random number generator though. Try
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char randChar;
    	srand( time(NULL) );
    	randChar = rand( ) % 26 + 'a';
    	putchar( randChar );
    	
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Ack, that was me.

  6. #6
    Unregistered
    Guest
    look at this
    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <dos.h>
    #include <conio.h>

    void showwhirl()
    {
        
    int ij;
        
    char whirl[] = {'|','/','-','\\'};

        for (
    i=0;i<=2i++)
        {
            for (
    j=0j<=3j++)
            {
                
    printf("%c"whirl[j]);
                
    delay(20);
                
    printf("\b");
            }
        }


    }

    void printline(char line[30])
    {
        
    int i;

        for (
    i=0line[i]; i++)
        {
            
    showwhirl();
            
    printf("%c"line[i]);
        }
    }



    main()
    {
        
    int i;

        
    randomize();

        
    clrscr();

        
    textcolor(LIGHTGREEN);

        do
        {
            for(
    i=1i<=40i++)
            {
                
    cprintf("%c "49 rand()%156);
            }

            
    delay(rand()%250);

        } while (!
    kbhit());

        
    clrscr();


        
    printline("Programmed By Maharshi Akilla");
        
    printf("\n");
        
    printline("http://mahurshi.tripod.com");

        
    getch();

        do
        {
        } while (!
    kbhit());

    A good example of what you can do with rand character generation..

    COOL PROGRAMS @ www.akilla.tk

  7. #7
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    What exactly is that supposed to do?
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  8. #8
    Unregistered
    Guest

    oops

    oops...

    that was..

    } while (!kbhit());

    good luck.

    COOL PROGRAMS @ www.akilla.tk

  9. #9
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ok thanks for the quick response I just posted like a min ago but anyways I fixed up the end of the program but there is a major flaw. One of the errors is an unexpected file end but I just put in the end } but then all of these:

    'empty character constant
    'delay' : undeclared identifier
    'randomize' : undeclared identifier
    'clrscr' : undeclared identifier
    'textcolor' : undeclared identifier
    'LIGHTGREEN' : undeclared identifier

    I know what the problem with all of these is(except for the empty character one)but in your program I am lost at how to find it, could you please help I am sorry about this but I don't know what is wrong.
    Last edited by CAP; 07-11-2002 at 10:58 AM.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  10. #10
    Unregistered
    Guest

    what exactly is that supposed to do ?

    what exactly is that supposed to do ?

    did you try running the code ??

    it prints random characters on the screen
    just like a screen saver (written by a beginner in c)

  11. #11
    Unregistered
    Guest

    and..

    oops... my above message is not intended for you..


    anyway, chances are that your compiler does not
    recognize the stuff that i put in my code..

    anway, the straight answer to your question has
    already been given by people in this post..

    the computer generates pseudo random numbers..

    to generate random characters, all you have to do
    is to generate random numbers which represent
    ascii values and then print the characters..

    that's what you are doing when you say
    printf("%c", 65);

    now, you can generate any number between 65 and 65+25
    (or a letter between a and z)
    by saying

    prinf("%c", 65+rand()%26)

    the randomize()
    function generates random numbers based on current time (this is supposed to generate 'better' random numbers)


    the whole point of the code is to show that by generating numbers, you can print them as characters and say "i have generated random characters"

    if you want that program to work, try compiling it with a different
    compiler... if you want to see how the exe works, go to http://mahurshi.tripod.com/software.htm and see "Matrix Show"

    good luck

    www.akilla.tk

  12. #12
    Unregistered
    Guest

    Lightbulb by the way

    and by the way, did you include all the required files from my code ??

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <dos.h>
    #include <conio.h>


    if it still doesn't work, it should work with a different compiler (try TC++ 3 if you have... or even Borland C++ 5.5 or something)

  13. #13
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ok, I am a total beginner so thanks for bearing with me. I am using Microsofts Introductory compiler if that makes any difference. Also I did go to your page(I said that above)and the matrix thing is really cool good job. I will try it on a different compiler when I get one(yes I know I can get them from here but I have to go)so anyways I did #include all of the right files and I will try it out later, thanks again.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  14. #14
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    I am at nowhere . I(honestly)have a friend who wanted me to make him like a program with all of the 0's and 1's after I showed him a program that kept repeating: Line(whatever line it was)but he didn't want one that simply put 10101001010110 or whatever cause it all looks the same, he wanted one that puts them in random order kinda like your matrix thing but with 0's and 1's, although the matrix one is cool. I don't know if that will help and I am asking cause I don't know how to make random things but I figured that I might as well try, so I hadn't started yet which is why I keep asking questions, sorry all.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
    	int r, i;
    	srand(time(NULL));
    	
    	for (i = 0; i < 8; i++)
    	{
    		r = rand() % 2; 
    		printf ("%d", r);
    	}
    	
    	return 0;
    }
    This prints a bunch of 1's and 0's.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  3. Random letters
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 06-09-2002, 11:35 PM
  4. generate random letters
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2002, 02:22 AM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM