Question regarding Random characters

This is a discussion on Question regarding Random characters within the C Programming forums, part of the General Programming Boards category; { int xx, yy, n, m; char zz1 = 65; char arr[6][6]; for (xx = 0; xx < 6; xx++) ...

  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,498
    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
    21,753
    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.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06: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, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21