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



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

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

Postby rakesh082 » Thu Nov 10, 2016 7:36 pm

Need help how to use RANDOM function to determine position of the needle and angle of the needle every time the needle is dropped.
rakesh082
 
Posts: 3
Joined: Thu Nov 10, 2016 7:30 pm
Has thanked: 2 times
Been thanked: 0 time

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

 

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

Postby Robert Sample » Thu Nov 10, 2016 8:12 pm

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.

These users thanked the author Robert Sample for the post:
rakesh082 (Thu Nov 10, 2016 10:21 pm)
Robert Sample
Global moderator
 
Posts: 3535
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 252 times

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

Postby rakesh082 » Thu Nov 10, 2016 9:44 pm

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.
rakesh082
 
Posts: 3
Joined: Thu Nov 10, 2016 7:30 pm
Has thanked: 2 times
Been thanked: 0 time

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

Postby Robert Sample » Thu Nov 10, 2016 11:09 pm

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).

These users thanked the author Robert Sample for the post:
rakesh082 (Mon Nov 14, 2016 5:41 pm)
Robert Sample
Global moderator
 
Posts: 3535
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 252 times

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

Postby NicC » Fri Nov 11, 2016 3:58 pm

Please do not post the same questions on multiple forums.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 2820
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 118 times

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

Postby rakesh082 » Mon Nov 14, 2016 5:41 pm

Hi Robert,

Thank you very much.
rakesh082
 
Posts: 3
Joined: Thu Nov 10, 2016 7:30 pm
Has thanked: 2 times
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post