10

I am trying to generate random data with range of specific latitude and longitude. The code below executes with no errors and also generates output but not fully what I am expecting.

Program:

import random
import sys
import math

latitude = 19.99
longitude = 73.78
output_file = 'filename'

def generate_random_data():
    with open(output_file, 'w') as 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 but gives only one row (which could be indentation problem) But this does not give me random lat & long in required Geo location as mentioned above.

Output generated:

000000000020 95.0 3.929691 1.749931

Output Expected: 000000000020 95.0 19.999691 73.799931

And this generates only one row where as I am trying to have million rows

9
  • 1
    The random generation looks right - the thing that might be contributing to the problem is the fact that "output_file" is never defined (and the function is never called). Commented May 14, 2015 at 20:13
  • 1
    @Scott The program is edited as it did show error for the point you raised. Commented May 14, 2015 at 20:38
  • 1
    @Scott col[0] and col[1] are correct but col[2] and col[3] are 4.484670 and 1.712346 are wrong I am trying to generate in a given range of lat and long of 19.99 and 73.78 respectively and also I am getting only one row. I did change the indentations of the output.write Commented May 14, 2015 at 20:48
  • 2
    It could help if in your post you create some expected output (range of lat, lon, how many rows, etc.). Commented May 14, 2015 at 20:50
  • 2
    Why not use a specialized package to generate fake data, so you can focus on other more important parts. I would recommend using Faker Commented May 14, 2015 at 21:20

4 Answers 4

11

I think this is what you are trying to do. I understand you are writing to a file; I took that off here just for the sake of this example.

import random
import sys
import math

latitude = 19.99
longitude = 73.78

def generate_random_data(lat, lon, num_rows):
    for _ 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: So your function including writing to file would then be the 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):
    with open(file_name, 'w') as output:
        for _ 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 the data posted above the EDIT into a file named 'random_lat_lon.txt'.

Sign up to request clarification or add additional context in comments.

3 Comments

@SitzBlogz One last thing: If your intention is that hex1 is a unique identifier you won't quite get that with '%012X' % random.randint(0,100) . If you want 12 character random strings do this instead: '%012x' % random.randrange(16**12). In fact I'll update my answer to include that.
can we have time series also randomly generated? Like start time HH:MM:SS and end time HH:MM:SS ? I mean to say in two new columns.
I started writing something but then searched and found stackoverflow.com/a/553320/4663466. So you could do something like s = '07:00:00' e = '16:45:00' f = '%H:%M:%S' and strTimeProp(s, e, f, random.random()). Which for example could give you 12:39:14. If you have any trouble integrating this into the function above post it as a new question and I'll try to answer before someone else :)
2

You need to call your fonction somewhere.

Generally, in Python you do something like:

if __name__ == '__main__':
  generate_random_data()

3 Comments

Output_file is not defined error after this function called
So..define it. output_file = "toto.txt" Plus the random() functiontakes no argument.
Thanks .. initial issue solved program is edited pls check once
1

I modified @Scott's response to create a CSV instead of a txt file:

import random
import sys
import math

latitude = 0.794501
longitude = -0.752568
file_n = 'random_lat_lon.csv'

def generate_random_data(lat, lon, num_rows, file_name):
    with open(file_name, 'w') as output:
        for _ 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, 600, file_n)

Comments

0

This is my version of this using NumPy:

import numpy as np

def random_lat_lon(n=1, lat_min=-90., lat_max=90., lon_min=-180., lon_max=180.):
    """
    this code produces an array with pairs lat, lon
    """
    lat = np.random.uniform(lat_min, lat_max, n)
    lon = np.random.uniform(lon_min, lon_max, n)

    return np.array(tuple(zip(lat, lon)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.