Thread: Newbie question

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

    Newbie question

    I'm new to this but i'm doing my best. Anyway i cant get the random function to work properly. I know it is in the FAQ but it doesnt help me.

    Can you tell me what is wrong with this code?

    Code:
     #include <iostream.h>
    #include <stdlib.h>
    
    
    int main()
    
    {
    
      int numbers[6], loop; 
      
      for (loop=0; loop<6; loop++)
      
      {
       
       numbers[loop]=rand();
        
      }
        
        
    
    return 0;
    
    }
    Anyone? Thanks.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Newbie question

    You have to seed the random generator before using it. You may have to include time.h.
    Also, rand() generates a random number in the range 0 - VERYLARGE, so if you just want a number say 0 - 9, use modulus, ie: rand() % 10.
    Originally posted by uNreal
    Code:
     #include <iostream.h>
    #include <stdlib.h>
    
    
    int main()
    
    {
      srand(time(NULL));
    
      int numbers[6], loop; 
      
      for (loop=0; loop<6; loop++)
      
      {
       
       numbers[loop]=rand();
        
      }
        
        
    
    return 0;
    
    }
    Anyone? Thanks.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    well, i look at it and see nothing wrong with it. it creates an array of 6 ints and stores random numbers in each. it has a few minor flaws, but it will compile and run and work. what do you think is wrong with it?
    hello, internet!

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    2
    Ok thanks Magos i think that solved it.

    moi the compiler wouldn't compile.........it didnt like it at all.

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM