Thread: n00b rand question

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    Question n00b rand question

    I'm a C programming class, and I have an assignment due tonight at midnight, but I can't get my rand() to work. I know its a little stupid thing, but it baffles me.

    Here is are the parameters given to us by the professor:

    I need a random number between 0 and 63.
    Using a defined RANGE value (set at 64) and using the modulus (%) operator, ensure that the value is different every time the program is started and can include 0 and 63 as values.
    Also, you must use the srand(time(null)) seed function.

    all in all, I need something that doesn't require making a new .h file or using two void satements. and the random number has to be set to x.

    I pray someone can help me fast.
    thanks all.

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    Thanks a lot, but I still can't get it to only pick numbers between 0 and 63. I'm so stupid.
    Last edited by SINISTAR556; 02-13-2005 at 08:55 PM.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    It's all in the FAQ andyhunter posted. If you still can't get it to work, then I'm sorry to say that there is no hope for you.

  5. #5
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Quote Originally Posted by SINISTAR556
    Thanks a lot, but I still can't get it to only pick numbers between 0 and 63. I'm so stupid.
    How about you show us what you've done so far?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    23
    Code:
    
    #include <time.h>
    #include <stdlib.h>
    
    int
    main(int argc, char **argv)  {
    
           int random = 0;
          /* initialize the random starting pointer */
         srand(time(NULL));
    
          ... statements here  ...
    
         /* generate a random number */
          random = rand()%64;
          ...   statements ...
          return 0;
    }
    There, that should work. All it does is modulus the output from rand() by 64, there fore making the number only be between 0 and 63 (including those)

    hope youmeant tonight at midnight...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. n00b questions
    By C+noob in forum C++ Programming
    Replies: 43
    Last Post: 07-09-2005, 03:38 PM
  2. n00b problems
    By piebob in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2005, 01:51 AM
  3. n00b Code Error.
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2004, 05:15 PM
  4. n00b doing a Socket operation on non-socket
    By Kinasz in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-25-2004, 03:29 AM
  5. ISO someone daring to look at some n00b code!
    By Rev. Jack Ed in forum Game Programming
    Replies: 4
    Last Post: 10-17-2003, 08:45 AM