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

In summary, the conversation discusses a problem with using rand() function to generate random numbers. It is pointed out that the assumption of the function returning a number between 0 and 32767 is incorrect as most Linux implementations return a number between 0 and 2147483647. Two solutions are suggested - dividing by RAND_MAX instead of 32767 or using a different random number generator.
  • #1
nenyan
67
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
  • #2
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.
 
  • #3
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.
 

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

1. Why does the code work in Windows but not in Linux?

There could be several reasons for this. One possible reason could be that the code was developed specifically for the Windows operating system and may not be compatible with Linux. Another reason could be that the code uses libraries or functions that are only available in Windows and not in Linux.

2. Can the code be modified to work in Linux?

Yes, it is possible to modify the code to make it compatible with Linux. This may require changing certain functions or using alternative libraries that are available in Linux. However, it may also depend on the complexity of the code and the specific features of the Linux system.

3. Is there a specific version of Linux that the code will work on?

It is difficult to say without knowing the specific details of the code. Some versions of Linux may have better compatibility with Windows programs, such as Wine or CrossOver, which could potentially allow the code to work. It is recommended to test the code on different versions of Linux to determine which one it is most compatible with.

4. Are there any general tips for making code compatible with different operating systems?

One tip is to use cross-platform libraries and functions that are available in both Windows and Linux. Another tip is to avoid using hard-coded paths or file names, as they may be different in different operating systems. It is also important to thoroughly test the code on different systems to identify any compatibility issues.

5. Will the code work in other operating systems besides Windows and Linux?

It depends on the specific operating system and the compatibility of the code with that system. Some operating systems, such as macOS, are based on Unix and may have better compatibility with Linux programs. However, it is recommended to test the code on different systems to ensure compatibility.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
737
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
Back
Top