Page 1 of 1

Cobol program that can estimate PI by ‘Buffon’s needle'

PostPosted: Thu Nov 10, 2016 7:36 pm
by rakesh082
Need help how to use RANDOM function to determine position of the needle and angle of the needle every time the needle is dropped.

Re: Cobol program that can estimate PI by ‘Buffon’s needle'

PostPosted: Thu Nov 10, 2016 8:12 pm
by Robert Sample
This doesn't seem like a very good problem to attack with COBOL, but so be it.

You'll need at least 3 random numbers -- the first two being the x and y coordinates for one end of the needle, scaled to fit your plane; the third being a (positive OR negative) value of the cosine of the angle between the needle and the x axis. From those values, you can calculate whether or not there is an intersection.

Re: Cobol program that can estimate PI by ‘Buffon’s needle'

PostPosted: Thu Nov 10, 2016 9:44 pm
by rakesh082
Thanks Robert.
I have some data like

Distance between lines = 100
Niddle length = 40
number of times dropped = 1000

I have never used Random function to calculate angle and there i am getting problem. if i am using 3 random numbers what will be the seed value for random function.

Re: Cobol program that can estimate PI by ‘Buffon’s needle'

PostPosted: Thu Nov 10, 2016 11:09 pm
by Robert Sample
The Enterprise COBOL Language Reference manual tells you plenty about the seed for RANDOM:
The seed value can be whatever you want between 0 and 2147483645 (in COBOL 5.1).
The seed value is only needed for the first use of RANDOM; after that, if no parameter is passed to RANDOM the next value in the current sequence will be returned.
If you don't specify a seed value the first call, RANDOM uses zero.
For any given seed value, the pseudorandom sequence returned will be the same.

So for simplicity of testing you probably want to use a constant for the seed value; for your actual run(s) you probably want to use something that will vary (based on time, for example -- calculate seconds elapsed today / this week / this month or this year). Note that the value returned by RANDOM needs to be stored in a COMP-1 or COMP-2 variable since it represents a floating point value between 0 and 1.

Cosine varies from +1 to -1 (which is a range of 2), so convert the pseudorandom value by multiplying it by 2 and subtracting 1 from it (this converts a value in the range 0 to 1 to a value in the range -1 to +1).

Re: Cobol program that can estimate PI by ‘Buffon’s needle'

PostPosted: Fri Nov 11, 2016 3:58 pm
by NicC
Please do not post the same questions on multiple forums.

Re: Cobol program that can estimate PI by ‘Buffon’s needle'

PostPosted: Mon Nov 14, 2016 5:41 pm
by rakesh082
Hi Robert,

Thank you very much.