Creating a distribution with specific mean and variance in FORTRAN 90

In summary, the conversation is about creating a normal distribution with a mean of 0.5 and variance of 0.05 using FORTRAN 90. The suggested method is to use the Box-Muller transform, which can be easily ported to any computer language. A C source code for this method is provided as well.
  • #1
sue132
14
0
Hi,

I'm trying to create a normal distribution with mean 0.5 and variance 0.05. I tried it initially with MATLAB, for which I used
Code:
newdist=0.5+(randn(1,1000)*sqrt(0.05));

Could you please help me in doing this in FORTRAN 90? Would generating a sequence using RANDOM_NUMBER and using the above equation give me similar results?

Thank you.

(P.S. : The LINUX OS on my system needs to be replaced, and I'm writing some more code before I can run them on another system. It would be great to have your help in the meanwhile, so I could check everything together. Thanks again)
 
Technology news on Phys.org
  • #2


You can't do it directly with random_number, which has a uniform distribution. There are different ways to produce a normal distribution from a uniform distribution, such as the Box-Muller transform.
 
  • #3
Hi sue132! :smile:

http://www.ohio.edu/people/just/Escalate/source/random.ccsome C source code for nrandom that generates normally distributed random numbers (based on the Box-muller transform).

The algorithm is easy to port to any computer language.
It comes from Numerical Recipes in C, for which there is also a FORTRAN version.
 
  • #4


Thank you for the replies
 
  • #5


Hi,

Creating a normal distribution with specific mean and variance in FORTRAN 90 can be achieved by using the RANDOM_NUMBER subroutine and the formula for calculating a normal distribution. Here is an example code that you can use:

PROGRAM normal_dist
IMPLICIT NONE

! Define variables
INTEGER :: i, n = 1000
REAL :: mean = 0.5, variance = 0.05
REAL :: x(n), u(n), v(n)

! Generate n random numbers between 0 and 1
CALL RANDOM_NUMBER(u)

! Calculate the standard normal distribution using Box-Muller transform
DO i = 1, n, 2
v(i) = SQRT(-2.0 * LOG(u(i))) * COS(2.0 * PI * u(i + 1))
v(i + 1) = SQRT(-2.0 * LOG(u(i))) * SIN(2.0 * PI * u(i + 1))
END DO

! Calculate the desired normal distribution
x = mean + SQRT(variance) * v

! Print the results
WRITE(*,*) "Generated normal distribution with mean = ", mean, " and variance = ", variance
WRITE(*,*) "First 10 values: ", x(1:10)

END PROGRAM normal_dist

This code uses the Box-Muller transform to generate a standard normal distribution and then scales it according to the desired mean and variance. You can change the values of mean and variance to get different distributions.
I hope this helps. Happy coding!
 

1. How do I specify the mean and variance in FORTRAN 90 when creating a distribution?

To specify the mean and variance in FORTRAN 90, you can use the random_number function to generate a set of random numbers and then use the mean and std_dev functions to calculate the desired mean and variance respectively. Alternatively, you can use the gauss function to directly generate a Gaussian distribution with a specified mean and variance.

2. Can I create a distribution with non-normal or non-Gaussian shape in FORTRAN 90?

Yes, FORTRAN 90 provides various built-in functions for generating distributions with different shapes and characteristics. For example, you can use the random_number function with a beta or gamma distribution to create non-normal shapes.

3. How accurate are the distributions created in FORTRAN 90?

The accuracy of the distributions created in FORTRAN 90 depends on the quality of the underlying random number generator used by the random_number function. This function uses a 32-bit linear congruential algorithm, which may not be suitable for all applications. You may consider using a different random number generator or implementing your own algorithm for improved accuracy.

4. Can I specify a range for the values in the distribution when using FORTRAN 90?

Yes, you can specify a range for the values in the distribution by using the scale and shift parameters in the random_number function. These parameters allow you to scale and shift the generated values to fit within a desired range.

5. Are there any limitations to creating distributions in FORTRAN 90?

There are some limitations when creating distributions in FORTRAN 90. Firstly, the random_number function only generates single-precision floating-point numbers, so the accuracy may be limited. Additionally, the built-in distribution functions only support a limited number of distributions, so you may need to implement your own algorithms for more complex distributions.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Advanced Physics Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
7
Views
6K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top