rand seed in C (explain a code)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalar
    New Member
    • Aug 2007
    • 82

    rand seed in C (explain a code)

    Hello everyone,I don't speak very good english , so sorry if i make any mistake
    Can anyone explain me this code
    [PHP]unsigned int rand_seed = 1;

    void srand(unsigned int seed) {
    rand_seed = seed;
    }

    int rand(void) { /*i don't understand what code do here down*/
    return ((rand_seed = rand_seed * 1103515245 + 12345 ) >> 16) & 0x7fff;
    }[/PHP]

    I know that if we want to change the seed with time, we write [PHP]srand((int)time (NULL))[/PHP]

    ps:If this thread is not in the right section please move it
    Thanks in advance
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by kalar
    Hello everyone,I don't speak very good english , so sorry if i make any mistake
    Can anyone explain me this code
    [PHP]unsigned int rand_seed = 1;

    void srand(unsigned int seed) {
    rand_seed = seed;
    }

    int rand(void) { /*i don't understand what code do here down*/
    return ((rand_seed = rand_seed * 1103515245 + 12345 ) >> 16) & 0x7fff;
    }[/PHP]

    I know that if we want to change the seed with time, we write [PHP]srand((int)time (NULL))[/PHP]

    ps:If this thread is not in the right section please move it
    Thanks in advance
    It's the right forum, no need to worry. You were looking at the source code for
    the rand() pseudo random number generator. It's a linear congruence method.
    Read all about that method here.

    kind regards,

    Jos

    Comment

    • kalar
      New Member
      • Aug 2007
      • 82

      #3
      So if understand good ,it's a method to generate random numders?
      In the link that you gave me has a type about this method with X ,c and others.So in my case .We choose some numbers like 1103515245.How this choice made? maybe depends on computer?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by kalar
        So if understand good ,it's a method to generate random numders?
        In the link that you gave me has a type about this method with X ,c and others.So in my case .We choose some numbers like 1103515245.How this choice made? maybe depends on computer?
        Yep, it generates 'pseudo random numbers'; the choice of these funny numbers
        is crucial to the quality of these pseudo random numbers. Here is a nice link.
        And no, those numbers are hardly related to the computer you use.

        kind regards,

        Jos

        Comment

        • kalar
          New Member
          • Aug 2007
          • 82

          #5
          Thank you
          very much

          Comment

          Working...