Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The code work in windows system but does not work in linux

  1. Mar 19, 2012 #1
    It looks like dead loop when it is running on linux system.
    It works perfectly in windows.

    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define NF 10  //total number of data.

    /* function prototypes */

    double gaussian( void );

        int i;
        double *ave;

        if(! (ave = malloc( NF * sizeof(double))) )
                printf("memory error \n");

            printf("%e, ", ave[i]);



    gaussian( void )
      static int useold = 0;
      static double old;
      if ( useold ) {
        useold = 0;
        return old;
      } else {
        double x, y, r2, norm;
        int j;
        double RAND;
        do {
          x = 2.0*RAND - 1.0;
           y = 2.0*RAND - 1.0;
          r2 = x*x + y*y;
        } while ( r2 >= 1.0 || r2 == 0.0 );
        norm = sqrt( -2.0*log( r2 )/r2 );
        old = x*norm;
        useold = 1;
        return y*norm;

  2. jcsd
  3. Mar 19, 2012 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    The problem is the way you are using rand(). You are assuming it returns a number between 0 and 32767. Most Linux implementations of rand() return a number between 0 and 2147483647.

    Two solutions:

    #1: You should be dividing by RAND_MAX instead of 32767. On a Linux box, that division by 32767 means your function guassian() will be looping for a long, long, long time.

    #2: Even better, don't not use rand(). It is at best a lousy pseudo random number generator, and in some implementations (e.g., microsoft) it is a very bad pseudo random number generator.
  4. Mar 19, 2012 #3
    Thank you very much. I chose the first solution. It's the most easy way~

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - code windows system Date
What should be the rules to write excellent code? Mar 12, 2018
Converting a DFA to efficient code? Mar 11, 2018
Python How can I run Python by getting values from Matlab Mar 7, 2018
Linux in Windows Feb 23, 2018
PID Codes Feb 22, 2018