Simple Fortran Program With Random Numbers

In summary: So the loop continues forever.In summary, the conversation discusses a problem with generating random x and y co-ordinates using FORTRAN's random number generator. The output is not being printed due to a potential issue with the compiler. The conversation also includes suggestions for fixing the problem by changing the logic in the code.
  • #1
ƒ(x) → ∞
25
0
I have been busy trying to generate, using FORTRAN's random number generator, random x and y co-ordinates which follow a simple pattern such as x2-100x, but for some reason the FORTRAN compiler prints nothing.

Anybody want to help me with this problem?

Code:
	program quadraticdistribution
	implicit none
	integer i
	integer x,y
	real l
	integer seed/-23434567/
c     ************************************
c     intialize the random number
      l=rand(seed)

c     intialize the x and y values
      x=0
      y=0
       
30      do i=1,200
            x=int(100*rand(0))
            y=int(100*rand(0))
        if (x.ge.(y*y-100*y)) goto 30
            Write(*,*) x,y
        enddo
           
           stop
           end
 
Technology news on Phys.org
  • #2
What compiler are you using? A lot of random stuff I've seen is compiler-specific. As a G95 user, I'm not familiar with your approach.
 
  • #3
Since you're not getting any output, it's probably the case that your if statement is preventing the following write statement from executing. I added a write statement right after x and y get set, so you can at least get some output.

The main problem, I believe, is your goto in the middle of your loop. I changed the logic in your if statement so that if x < y^2 - 100y, the code prints the values of x and y. If x >= y^2 - 100y, the code doesn't print anything.
Code:
	program quadraticdistribution
	implicit none
	integer i
	integer x,y
	real l
	integer seed/-23434567/
c     ************************************
c     intialize the random number
      l=rand(seed)

c     intialize the x and y values
      x=0
      y=0
       
      do i=1,200
         x=int(100*rand(0))
         y=int(100*rand(0))
         write(*, *) x, y   ; for debugging purposes
         if (x .lt. (y*y-100*y)) 
            Write(*,*) x,y
         endif
        enddo
           
           stop
           end
 
  • #4
If y lies between 0 and 100, y^2-100*y is always negative, and the condition x>y^2-100*y is always true.
 
  • #5



Hello, it seems like you are having trouble with your FORTRAN program that generates random numbers with a specific pattern. There could be a few reasons why your program is not working as expected. First, make sure that you have correctly declared your variables and initialized the random number generator. In your program, you have declared the variables for x and y as integers, but you are using the "rand" function which generates real numbers. This could cause issues with your program.

Also, it is recommended to use a different seed value for each run of your program to ensure a different set of random numbers is generated each time. Right now, you have set the seed to a fixed value of -23434567, which may not be ideal.

Another potential issue could be with your loop. Your code suggests that you want to generate 200 pairs of x and y coordinates, but your loop only runs for 30 iterations. This could be due to the "goto 30" statement, which may be causing your loop to repeat the same values.

I suggest reviewing your code and making sure that all variables are correctly declared and initialized, and that your loop is set up to generate the desired number of coordinates. If you are still having issues, please provide more details about the specific problem you are facing, and I would be happy to assist you further. Best of luck with your FORTRAN program!
 

FAQ: Simple Fortran Program With Random Numbers

1. What is a simple Fortran program with random numbers?

A simple Fortran program with random numbers is a computer program written in the Fortran programming language that uses a random number generator to produce random values. This can be useful for simulating random events or generating test data.

2. How do I generate random numbers in a Fortran program?

In Fortran, random numbers can be generated using the RANDOM_NUMBER function. This function takes in an array of numbers and fills it with random values between 0 and 1. These values can then be manipulated as needed in the program.

3. Can a Fortran program generate specific types of random numbers?

Yes, the RANDOM_NUMBER function in Fortran allows for the generation of different types of random numbers, such as integers, real numbers, and even complex numbers. The type of random number can be specified by the programmer when calling the function.

4. Is it possible to control the range of random numbers in a Fortran program?

Yes, the RANDOM_NUMBER function in Fortran allows for the specification of a range for the random numbers to be generated in. This can be done by multiplying the output of the function by the desired range and adding the minimum value. For example, to generate random numbers between 1 and 10, you would multiply the output by 9 and add 1.

5. How can I ensure that my Fortran program generates truly random numbers?

Fortran does not have a built-in function for generating truly random numbers, but it does have a pseudo-random number generator (PRNG) that produces a sequence of numbers that appear random. To ensure a more random sequence, the programmer can set a seed value for the PRNG using the RANDOM_SEED function, which will change the starting point of the sequence. Additionally, Fortran also has libraries and modules available for use that can generate more complex and unpredictable random numbers.

Similar threads

Replies
4
Views
2K
Replies
2
Views
1K
Replies
4
Views
1K
Replies
5
Views
2K
Replies
1
Views
2K
Back
Top