# L-28 MCS 275 Wed 15 Mar 2017 : mc4pi_client.py
"""
This client applies Monte Carlo simulation to
estimate Pi.  The first seed comes from the
server in the program mc2pi2.py.
"""
import random
from socket import socket as Socket
from socket import AF_INET, SOCK_STREAM

HOSTNAME = 'localhost'  # on same host
PORTNUMBER = 11267      # same port number
BUFFERSIZE = 80         # size of the buffer

SERVER_ADDRESS = (HOSTNAME, PORTNUMBER)
CLIENT = Socket(AF_INET, SOCK_STREAM)
CLIENT.connect(SERVER_ADDRESS)

print('client is connected')
SEED = CLIENT.recv(BUFFERSIZE).decode()
print('client received %s' % SEED)

random.seed(int(SEED))

NBR = 10**7
CNT = 0
for i in range(NBR):
    XPT = random.uniform(0, 1)
    YPT = random.uniform(0, 1)
    if XPT**2 + YPT**2 <= 1:
        CNT = CNT + 1
RESULT = float(CNT)/NBR
print('client computes %.12f' % RESULT)

CLIENT.send(str(RESULT).encode())

CLIENT.close()
