help me to generate a random number on the basis of system time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • palani12kumar
    New Member
    • Oct 2006
    • 60

    help me to generate a random number on the basis of system time

    i want to generate a random number by using the system time. i dont know how to do it. And another thing is, how to find the millisecond? please help me
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Hi,

    Please check this thread.

    Random Numbers


    Regards,
    M.Sivadhas.

    Comment

    • palani12kumar
      New Member
      • Oct 2006
      • 60

      #3
      i asked that i need to generate a random number on the basis of system time

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Use this statement at the very beginning of your main() function:

        Code:
        srand((unsigned)time(0));
        In order to limit your values, use the modulus operator (%) with the range of values you wanted. Suppose you wanted a value between 0 and 50 (including 0, but not 50). Use the statement:

        Code:
        int x = rand() % 50;
        If you want to have a higher minimum value (such as 50 to 100 including 50 but not 100), use

        Code:
        int x = rand() % 50 + 50; // Generates random number between 0 and 50, then adds 50 to it

        Comment

        Working...