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

Click For Summary
SUMMARY

The code provided runs into an infinite loop on Linux systems due to incorrect assumptions about the output range of the rand() function. While it works on Windows, Linux implementations of rand() typically return values between 0 and 2147483647, not 32767. To resolve this, developers should divide by RAND_MAX instead of 32767, or better yet, replace rand() with a more reliable random number generator.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with memory management in C (e.g., malloc() and free())
  • Knowledge of random number generation techniques
  • Experience with cross-platform compatibility issues in software development
NEXT STEPS
  • Research RAND_MAX and its implications in C programming
  • Learn about alternative random number generators in C, such as random() or mt19937
  • Explore debugging techniques for identifying infinite loops in C code
  • Investigate cross-platform development practices to ensure compatibility between Windows and Linux
USEFUL FOR

Software developers, particularly those working with C programming, and anyone facing cross-platform compatibility challenges in their applications.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
3K
Replies
9
Views
2K