9

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

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • 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). – mprat May 14 '15 at 20:13
  • Are you sure this works? I don't recall random.random() taking any arguments. https://docs.python.org/2/library/random.html What do you want random.random(0,100) to output? Random ints from 0 to 100? – Scott May 14 '15 at 20:20
  • 1
    @Scott The program is edited as it did show error for the point you raised. –  May 14 '15 at 20:38
  • Running this code creates a file 'filename' with the following data: 000000000025 58.0 4.484670 1.712346. Is this expected output? – Scott May 14 '15 at 20:42
  • 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` –  May 14 '15 at 20:48
  • 2
    It could help if in your post you create some expected output (range of lat, lon, how many rows, etc.). – Scott May 14 '15 at 20:50
  • 1
    @Scott Done !! Thank you for pointing .. The lat n long range is given in program. –  May 14 '15 at 20:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77844/discussion-between-scott-and-sitz-blogz). – Scott May 14 '15 at 20:57
  • 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](http://www.joke2k.net/faker/) – Boggio May 14 '15 at 21:20

4 Answers4

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

Scott
  • 6,089
  • 4
  • 34
  • 51
  • 1
    @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. – Scott May 14 '15 at 22:48
  • 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. –  May 16 '15 at 05:05
  • 2
    I started writing something but then searched and found http://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 :) – Scott May 16 '15 at 05:32
2

You need to call your fonction somewhere.

Generally, in Python you do something like:

if __name__ == '__main__':
  generate_random_data()
David Dahan
  • 10,576
  • 11
  • 64
  • 137
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)
joshi123
  • 835
  • 2
  • 13
  • 33
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)))
Juliën
  • 9,047
  • 7
  • 49
  • 80
user_dhrn
  • 557
  • 1
  • 5
  • 18