Thread: Question regarding Random characters

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Question Question regarding Random characters

    {
    int xx, yy, n, m;
    char zz1 = 65;
    char arr[6][6];
    for (xx = 0; xx < 6; xx++)
    {
    for (yy = 0; yy < 6; yy++)
    {
    n=rand()%6;
    m=rand()%6;
    arr[n][m] = zz1+xx+yy;
    printf("%3c",arr[n][m]);
    }
    printf("\n");
    }
    printf("\n");
    }


    hi all i am new to C programming and i wish to create a 2D arrays with random characters by using ASCII codes.... but after a few tries i am not getting the output i want, the characters are created but it was not random at all >.< The output are shown below :
    ABCDEF
    BCDEFG
    CDEFGH
    DEFGHI
    EFGHIJ
    FGHIJK


    please help me i need your advice~

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please read the following page first, and fix your post to use code tags so people can read it more easily. Thanks.

    C Board - Announcements in Forum : C Programming

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First you need to use srand(time(0)); at the top of your code.

    Use the rand() function to generate characters not array coordinates.
    Use the loops to sequentially step through each element of the array sequentially.
    Code:
    a[x][y] = (rand() % 26) + 'A';
    Will probably do the job for you.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    thx for your help, but may i know what is the purpose we using:
    srand(time(0));
    the output seems to be repeatable, any possible way to make each character only appear once?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gami911
    but may i know what is the purpose we using:
    Code:
    srand(time(0));
    To provide the pseudorandom number generator with a seed that is different from each run of the program, and thus not end up with the same pseudorandom sequence on every run of the program. Read Prelude's article on using rand().

    Quote Originally Posted by gami911
    the output seems to be repeatable, any possible way to make each character only appear once?
    Given an array with the letters of the alphabet, shuffle the array and select the first 6 letters in the shuffled array. (Actually, you only need to shuffle the array by selecting letters at random until the first 6 letters have been selected at random.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Random question simulator
    By Pickpocket in forum C Programming
    Replies: 9
    Last Post: 12-04-2006, 07:13 AM
  4. quick question on random #'s
    By Tsukasa in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2005, 11:33 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM