Thread: Periodicity of rand

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

    Periodicity of rand

    Does anyone know what sort of periodicity rand has?

    I made a program to try and find a pattern but so far its on around 50billion itterations and I'm just wondering if this is going to go on for days or if my program isnt right and I have missed the pattern.

    Thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That is going to be compiler dependent... Some are crappy, some are excellent... most are somewhere in the middle.

  3. #3
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    I'm not sure how much this helps determine periodicity, but here is a way to at least get the same sequence...
    from rand(3): pseudo-random number generator - Linux man page

    Code:
    POSIX.1-2001 gives the following example of an implementation of rand()
           and srand(), possibly useful when one needs the same  sequence  on  two
           different machines.
    
               static unsigned long next = 1;
    
               /* RAND_MAX assumed to be 32767 */
               int myrand(void) {
                   next = next * 1103515245 + 12345;
                   return((unsigned)(next/65536) % 32768);
               }
    
               void mysrand(unsigned seed) {
                   next = seed;
               }
    Last edited by hellork; 11-24-2010 at 03:27 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    hmm i'm using GCC. I thought that the rand function was just defined in the stdlib.h library and so is just like a function that would be the same ran in any instance. Anyone got any ideas of a round about sort of period? I think it should depend on the seed though..

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I thought that the rand function was just defined in the stdlib.h library
    It is, but each compiler writer supplies their own IMPLEMENTATION of the code behind stdlib.h.
    You need to find glibc, and find out how it implements rand().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime problem
    By SantoshN in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 02:42 PM
  2. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  3. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM