rand....

This is a discussion on rand.... within the C Programming forums, part of the General Programming Boards category; okey I get how to use rand and srand but my tiny problem is that I cant seme to find ...

  1. #1
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259

    rand....

    okey I get how to use rand and srand but my tiny problem is that I cant seme to find any good seed for it Iīve pritty much figurd out that I should use the second on the clock since that is what I use when I use basic but the time comand or the tm_sec doesnt seme to work the way I whant them to....sure I get differnt numbers then I usaly do but they still arnīt randome so what seed should I use ?
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Most people go with this
    Code:
    #include <stdlib.h>
    #include <time.h>
    
    srand((unsigned)time(NULL));
    That easily sets the seed for the current time. time returns a time_t value, and srand takes an unsigned int, so I like to cast to make sure everything's kosher.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Code:
    srand(time(NULL));

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Shogun: Maybe you should read the FAQ
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    I took a look at the faq now and found alot of nice stuff
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  6. #6
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    You may also try this code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    /***************************************************
    * function to generate numbers between 1 and "max" *
    ***************************************************/
    int my_gen(int max)
    {
       return 1+rand()%(max);
    }
    
    
    int main(void)
    {
       int number; 
    
       srand( time(NULL) ); /* activate randomizer */ 
    
       number = my_gen(20);
       printf(" Random number between 1 and 20: %i",number); 
    
       return 0;
    }

  7. #7
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    why doesnt this code work?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (){
    int i;
    srand (time (NULL));
    i=rand();
    printf("%d",i);
    return 0;
    }
    and yes I have read some manuals be4 coming asking this question.
    I get missing "(" when I compile it
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  8. #8
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    I tryed to make a new program rewriting the exact same code and it worked and it was the exact code since I compared them letter by letter. :confussed:
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  9. #9
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    ah okey I see but the thing is the comp I write C on doesnīt have any internet so I dont compy past from web pages or I can but it would be much work putting it on a disk and so so I simply just write the code myself and that still happend :still confussed:
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 06:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 02:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 10:13 PM

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