4 votes 4 votes main() { int c=0,x,i,y; for(i=0;i<N;i++) { x=random();y=random(); if((x2+y2)<1) c++; } print(c); } random() is a function which returns random number between $0$ or $1$, $N$ is a large number what is the output of the program? Programming in C programming-in-c iiith-pgee + – dragonball asked May 2, 2017 • retagged Jun 4, 2017 by Arjun dragonball 2.4k views answer comment Share Follow See all 24 Comments See all 24 24 Comments reply Shubhanshu commented May 2, 2017 reply Follow Share We can't predict the correct value of c because its value is entirely depend on the function random but we can define its range i.e. under what range the value does c will have after termination, which is (0 to N). 0 votes 0 votes dragonball commented May 2, 2017 reply Follow Share This is a pseudo code , using the random() function which is basically similar to rand() in C function . Just a note . Secondly , the answers were like converges to PI .. like this as I cannot remember the complete set of answers as it was asked this year in IIITH. 0 votes 0 votes Arunav Khare commented May 3, 2017 reply Follow Share Question says random() is a function which returns random number between 0 or 1 (Should be and, it is typo ?) which means it must be float / double. Since, x and y are ints, the return value would be typecasted to int. Which means floored. Since value is between 0 and 1, so it would be floored to zero always. This means, $x^2 + y^2$ would be $0$, which is less than $1$, so c would always be incremented. So value of 'c' would be N 0 votes 0 votes Shubhanshu commented May 3, 2017 reply Follow Share It means that x and y will always take the values between 0 and 1 but not 0 and 1 i.e. excluding the values 0 and 1 and including the value between them (float values). ryt? 0 votes 0 votes dd commented May 3, 2017 reply Follow Share almost similar question I posted last year : https://gateoverflow.in/93587/expected-value 1 votes 1 votes dragonball commented May 3, 2017 reply Follow Share But the option are like converges to PI , 3*PI and rest I can not remember . 0 votes 0 votes dd commented May 3, 2017 reply Follow Share what is PI ? 0 votes 0 votes dd commented May 3, 2017 reply Follow Share Ok ? is random returning float ?? greater than 0 and less than 1 ?? 0 votes 0 votes dragonball commented May 3, 2017 reply Follow Share I think PI means 3.14 (approx.) and there are nothing mentioned in the question like return type of random() . 0 votes 0 votes dd commented May 3, 2017 reply Follow Share is there anything like $\frac{PI}{4}\cdot N$ ?? 0 votes 0 votes dd commented May 3, 2017 reply Follow Share See, if only integer values are taking then N/4 will be the answer and if float values are taking then for $(x^2 + y^2) < 1$ is required area is $PI/4$ where the radius is = $1$ so the probability os $P( (x^2+y^2) < 1 ) = PI/4$. Using expected value summation rule c = $(PI/4)^* N.$ please correct if wrong. 1 votes 1 votes Sushant Gokhale commented May 3, 2017 reply Follow Share Expected value of (x2+y2) = E(x2+y2) = E(x2) + E(y2) = 2.E(x2) ....due to symmetry Now, as 'x' is assumed to takes integer value, either 0 or 1. It can take 1 only if random() returns 1 else it will take 0. Obviously, probablity of random() returning 1 is almost 0 and hence, E(x2)$\approx$ 0 As each time 'c' will be incremented, expected value of 'c' is N @Debashish. If we consider float values, E(x2) = $\int_{0}^{1}x^{2}.f(x)$ = $\int_{0}^{1}x^{2}.\frac{1}{1-0}$ ...uniform distribution = $\frac{1}{3}$ Thus, E(x2+y2) = 2 * (1/3) = 2/3 Thus, E(c) = (2/3) * N What's wrong with this approach? 0 votes 0 votes dd commented May 3, 2017 reply Follow Share do we need expected value of x^2+y^2 ?? 0 votes 0 votes Sushant Gokhale commented May 3, 2017 reply Follow Share yes, right? 0 votes 0 votes dd commented May 3, 2017 reply Follow Share your first approach seems correct. Btw 2nd approach is wrong because we do not need expected value of $x^2+y^2$ 0 votes 0 votes dd commented May 3, 2017 reply Follow Share But I am having doubt on integer and float values. As you said the in the first approach it should be N. 0 votes 0 votes Sushant Gokhale commented May 3, 2017 reply Follow Share Ideally your probablity should be equal to my expected value for float values :) I dont know where I (or you) are going wrong 0 votes 0 votes Sushant Gokhale commented May 3, 2017 reply Follow Share @Debashish.Consider this. Even though probablity of area is $\frac{\pi }{4}$, how equipobable are all the points inside the area? I dont think they are equiprobable. For that I think, we need expected value concept. 0 votes 0 votes srestha commented May 3, 2017 reply Follow Share It is $x^{2}+y^{2}<1$ So, can we say it is obviously not a circle. Then how pi/4 comes as answer? 0 votes 0 votes dd commented May 3, 2017 reply Follow Share If you are talking about floating point returned by rand() ( no int conversion) they are uniform and hence coordinates are equiprobable. 0 votes 0 votes Dhruv Patel commented May 3, 2017 reply Follow Share @Sushant I don't understand why you are taking E($x^2 + y^2$) If we take $I_k$ be the indicator random variable which is 1 iff $x^2 + y^2 < 1$ in kth iteration, which happens with probability $\frac{\pi}{4}$ then E($I_1+I_2+ ... + I_N$) = $\frac{N*\pi}{4}$ 1 votes 1 votes Sushant Gokhale commented May 3, 2017 reply Follow Share @Dhruv. I accept my mistake :) I applied wrong concept. 0 votes 0 votes srestha commented May 3, 2017 reply Follow Share @Debashish I am in doubt how floating point could be applied here? U mean decimal value? 0 votes 0 votes A_i_$_h commented Jun 19, 2017 reply Follow Share how are u taking the probability of pi/4, can u please explain in a more simple way 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 votes This is monte carlo method to estimate value of pi. Value of c : N*π/4 For reference to monte carlo method: http://www.dartmouth.edu/~rc/classes/soft_dev/C_simple_ex.html Enyt_S answered Apr 22, 2019 Enyt_S comment Share Follow See all 0 reply Please log in or register to add a comment.