0

I am new to fortran and I am trying to write code using random data instead of binned data in x, y, z as shown in my sample code.

         implicit real*8(a-h,o-z)
     dimension rm(4),rp1(4),rip1(4),rp2(4),rip2(4),rp3(4),rip3(4),
 d   rn(4),u1(4),u2(4),u3(4)


    do ix= 1000,25000,1000
    x = ix/1000000.
    do iy= 1000,25000,1000
    y = iy/100000.
    do iz= 1,1000,25
    z = iz/10000.
    a=(x**2+y**2)/z
    b=x*y*z
    c=x*y**2+y*z**2+z*x**2
    fr=(a*b)/c

    if(fr.ge.0.05.and.fr.le.23)then
    write(40,*)x,y,x,fr
    else
    endif

    end do
    end do
    end do


    stop

How to convert such code having binned data to a code using random draws.

As an example binned data here means possible fixed values of x are {1000/1000000.,2000/1000000., .....,25000/1000000.} i.e. 25 possible values in range {.001, .025} but they are not random values

In case of random values 25 points will be drawn from the range {.001, .025} randomly.

This my assumption about doing the analysis with random draws(previously I was not familiar with this ).

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • 1
    First step use Implicit None. Second step learn about kinds and why real*8 is not good - https://stackoverflow.com/questions/838310/fortran-90-kind-parameter. Third step explain your question more fully - I have no idea what you mean by "using random datas instead of binned datas". A complete program that I can compile, link, run and compare the output with some test output you provide will really help. – Ian Bush Jan 20 '21 at 10:37
  • I think the code given is reproducible(reproduced a correct data file using codeblocks) and now I have written what exactly I needed – Dark Knight45 Jan 20 '21 at 11:57
  • Sorry - I had misread and thought there some uninitialised data. – Ian Bush Jan 20 '21 at 11:59

1 Answers1

0

Something like

ian@eris:~/work/stack$ cat data.f90
Program random_data

  Use, Intrinsic :: iso_fortran_env, Only :  wp => real64

  Implicit None

  Real( wp ), Parameter :: min_rand = 0.001_wp
  Real( wp ), Parameter :: max_rand = 0.025_wp

  Integer, Parameter :: n_samples = 25
  
  Real( wp ) :: x, y, z
  Real( wp ) :: a, b, c
  Real( wp ) :: fr

  Integer :: i_sample

  Do i_sample = 1, n_samples

     Call Random_number( x )
     Call Random_number( y )
     Call Random_number( z )

     x = x * ( max_rand - min_rand ) + min_rand
     y = y * ( max_rand - min_rand ) + min_rand
     z = z * ( max_rand - min_rand ) + min_rand
  
     a=(x**2+y**2)/z
     b=x*y*z
     c=x*y**2+y*z**2+z*x**2
     fr=(a*b)/c

     If( fr >= 0.05_wp .And. fr <= 23.0_wp )Then
        Write( 40, * ) x, y, x, fr
     Endif

  End Do

End Program random_data
ian@eris:~/work/stack$ gfortran-10 -Wall -Wextra -fcheck=all -std=f2008 -g -finit-real=snan data.f90 
ian@eris:~/work/stack$ ./a.out;more fort.40
more: stat of fort.40 failed: No such file or directory

Unfortunately none of the random numbers in this run produced an output that lay in the desired range - however I did test it with 2500 samples and then a couple did.

Ian Bush
  • 6,996
  • 1
  • 21
  • 27
  • Your code shows a lot of errors like1. Error: Non-numeric character in statement label, 2.Error: Unclassifiable statement etc. – Dark Knight45 Jan 20 '21 at 12:24
  • 3
    It's free format Fortran, not archaic fixed format - you can see my successful compilation at the bottom – Ian Bush Jan 20 '21 at 12:48
  • Thanks a lot Ian. Did you know how to make" Integer, Parameter :: n_samples >1E+10", since beyond 2*1E+9 it is giving error messages – Dark Knight45 Jan 20 '21 at 21:26
  • It's best to ask a new question rather than have a discussion in the comments about a new topic - but this would just get marked as a duplicate - see https://stackoverflow.com/questions/23126363/how-to-overcome-integer-overflow-in-fortran-90 Again part of the solution is learning about kinds – Ian Bush Jan 20 '21 at 21:37