Thread: there's something wrong with my random numbers

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

    there's something wrong with my random numbers

    Hi, C Board

    I'm writing a program to simulate dropping particles onto a random place on a 1-dimensional surface.
    The heights at each point in the surface are stored in an array, and when a particle falls on a point, it increases the height at that point by one.

    The program is meant to do several simulations, with the total number of particles dropped varying each time.

    However, when plotted, the lines for each of these simulations clearly follows the same pattern. (see attached picture)
    Does anyone know why this is happening, and how to make it not happen?

    Thanks in advance, Dez.



    The relevant bit of code (WIDTH is 100):

    Code:
        
        srandom( time(NULL) );
    
        for ( drops=1000 ; drops<10000 ; drops+=1000 )
        {
    
            for ( l=0 ; l<drops ; l++ )
            {
                x = random();
                x = x%WIDTH ;
                surface[x]++ ;
            }
    
            for ( x=0 ; x<WIDTH ; x++ )
            {
                fprintf( p_outfile , "%lu," , surface[x] );
            }
            fprintf( p_outfile , "\n" );
    
        }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you're accumulating, not resetting, there shouldn't be huge differences from one line to the next, right? (For instance look around 86; in the second line from the top, there's a large dip there, but in the top blue line it gets leveled out, since that relatively low spot then got a few more added to it in the last set of 1000.)

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Aha, I see.
    That's pretty embarrassingly blindingly obvious.
    Thanks, man.
    Last edited by dez; 03-31-2010 at 07:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 12:29 PM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 01:22 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM