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

AI Thread Summary
The discussion centers on a C program that runs into issues on Linux but works fine on Windows, specifically experiencing a dead loop. The problem arises from the use of the `rand()` function, which is assumed to return values between 0 and 32767. However, most Linux implementations of `rand()` return values up to 2147483647. This discrepancy causes the Gaussian function to loop indefinitely on Linux. Two solutions are proposed: first, divide by `RAND_MAX` instead of 32767 to ensure compatibility across platforms; second, consider using a better random number generator than `rand()`, as it is often unreliable. The first solution is noted as the simpler fix.
nenyan
Messages
67
Reaction score
0
It looks like dead loop when it is running on linux system.
It works perfectly in windows.

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


/* function prototypes */

double gaussian( void );

int
main()
{
	int i; 
	double *ave; 
	

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

	for(i=0;i<NF;i++)
		ave[i]=gaussian();
	for(i=0;i<NF;i++)
		printf("%e, ", ave[i]);

	
	
free(ave);


}



double
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 {
		j=rand();
		RAND=j*1.0/32767;
      x = 2.0*RAND - 1.0;
	  j=rand();
		RAND=j*1.0/32767;
       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;
  }
}
 
Technology news on Phys.org
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.
 
Thank you very much. I chose the first solution. It's the most easy way~

D H said:
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top