Fortran Simple Fortran Program With Random Numbers

Click For Summary
The discussion revolves around troubleshooting a FORTRAN program designed to generate random x and y coordinates based on a quadratic equation. The original code fails to produce output, likely due to an issue with the if statement and the use of a goto statement within the loop. A user suggests modifying the logic to ensure that values of x and y are printed when the condition is met, rather than relying on a goto that could prevent execution of the write statement. It is noted that if y is constrained between 0 and 100, the expression y^2 - 100y will always yield a negative result, making the condition x >= y^2 - 100y perpetually true, which further complicates the output. The conversation highlights the importance of understanding compiler-specific behaviors and debugging techniques in FORTRAN programming.
ƒ(x) → ∞
Messages
24
Reaction score
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
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.
 
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
 
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
22
Views
5K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 19 ·
Replies
19
Views
6K