I really need to get this working =(

This is a discussion on I really need to get this working =( within the C Programming forums, part of the General Programming Boards category; ok im tring to make this print a randem number from 0 to 52 but i want it print out ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    rand()/linux

    ok im tring to make this print a randem number from 0 to 52 but i want it print out a diffrent rand everything i re run the prog.. can someone tell me how to do this.

    Code:
    #include<stdio.h>
    
    int main()
      {
             int x[52];
             int round;
    
             for(round = 0; round < 10; round++);
             x[round] = (rand() % 51) + 1;
    
              printf(" %d\n",x[round]);
              return 0;
      }
    Last edited by xlordt; 10-11-2002 at 01:49 AM.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    aaahhh you mean like this.....

    Code:
    int main()
      {
             int x[52];
             int round;
    
             for(round = 0; round < 10; round++)
             {
             x[round] = (rand() % 51) + 1;
    
              printf(" %d\n",x[round]);
    
              return 0;
             }
      }
    but how do i make it pring out diffrent number everything i re execute it instead of 11 all the time
    Last edited by xlordt; 10-11-2002 at 01:56 AM.

  3. #3
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    Code:
      int main()
      {
             int x[52];
             int round;
    
             for(round = 0; round < 10; round++)
             {
             x[round] = (rand() % 51) + 1;
    
              printf(" %d\n",x[round]);
    
              return 0;
             }
      }
    Is the return in the wrong place? It seems to me that the loop will enter, run once, and then the program will end returning the value of 0(or success)?
    Or have I not mastered the int main() return value yet?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by VegasSte
    Is the return in the wrong place? It seems to me that the loop will enter, run once, and then the program will end returning the value of 0(or success)?
    Or have I not mastered the int main() return value yet?
    Yep. It's in the wrong place. Depending on your compiler options, you should get an error, something like: "Not all paths return a value." ... something to that effect.

    In a function that returns a value, all 'paths' need to return something.
    Code:
    int func( int x )
    {
        if( x == 10 )
        {
            return 5;
        }
        /* should give a warning here since
        there is no guarinteed return result
        for this function. ie: if x is not 10,
        then nothing gets returned */
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Before you use the rand function, you need to call the srand function.
    Code:
    srand( (unsigned)time( NULL ) );

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>x[round] = (rand() % 51) + 1;
    http://www.eskimo.com/~scs/C-faq/q13.16.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Post program

    Here is what the program sould look like:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define max_rand 52
    
    int main()
    {
       int rand_num[max_rand];
       register int c;
       srand(time(NULL));
       for(c = 0; c < max_rand; c++)
       	rand_num[c] = (rand() %51) + 1;
       for(c = 0; c < max_rand; c++)
       	printf("%d\n", rand_num[c]);
       return 0;
    }
    You didn't seed the random generator, and you would only print out the last number entered with your printf() function

    [edit]Code tags added by Hammer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 04:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 08:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 05:08 AM

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