python - Generate random data with lat-long -
i trying generate random data range of specific latitude , longitude. code below executes no errors , generates output not expecting.
program:
import random import sys import math latitude = 19.99 longitude = 73.78 output_file = 'filename' def generate_random_data(): open(output_file, 'w') output: hex1 = '%012x' % random.randint(0,100) flt = float(random.randint(0,100)) latitude = math.acos(random.random() * 2 - 1) longitude = random.random() * math.pi * 2 output.write('%s %.1f %.6f %.6f \n' % (hex1.lower(), flt, longitude, latitude)) if __name__ == '__main__': generate_random_data()
the above code runs gives 1 row (which indentation problem) not give me random lat
& long
in required geo location mentioned above.
output generated:
000000000020 95.0 3.929691 1.749931
output expected: 000000000020 95.0 19.999691 73.799931
and generates one
row trying have million
rows
i think trying do. understand writing file; took off here sake of example.
import random import sys import math latitude = 19.99 longitude = 73.78 def generate_random_data(lat, lon, num_rows): _ in xrange(num_rows): hex1 = '%012x' % random.randrange(16**12) # 12 char random string flt = float(random.randint(0,100)) dec_lat = random.random()/100 dec_lon = random.random()/100 print '%s %.1f %.6f %.6f \n' % (hex1.lower(), flt, lon+dec_lon, lat+dec_lat) generate_random_data(latitude, longitude, 5)
prints:
31d831302b7f 99.0 73.789561 19.997404 c6ef41c70ebb 0.0 73.780732 19.994279 4bc2df5388e3 77.0 73.785531 19.994191 e7648f673475 40.0 73.786610 19.993679 a11502c744a6 32.0 73.784650 19.997702
edit: function including writing file following
import random import sys import math latitude = 19.99 longitude = 73.78 file_n = 'random_lat_lon.txt' def generate_random_data(lat, lon, num_rows, file_name): open(file_name, 'w') output: _ in xrange(num_rows): hex1 = '%012x' % random.randrange(16**12) flt = float(random.randint(0,100)) dec_lat = random.random()/100 dec_lon = random.random()/100 output.write('%s %.1f %.6f %.6f \n' % (hex1.lower(), flt, lon+dec_lon, lat+dec_lat)) generate_random_data(latitude, longitude, 5, file_n)
which generates data posted above edit file named 'random_lat_lon.txt'.
Comments
Post a Comment