James Hampton
James Hampton

Reputation: 21

Basic Python programming help needed involving arrays and random locations

Consider a 100X100 array.

  1. Generate an array of several thousand random locations within such an array, e.g. (3,75) and (56, 34).
  2. Calculate how often one of your random locations falls within 15 pixels of any of the (straight) edges

I am trying to do the above question in order to help me to learn the programming language Python, i am new to programming.

Here is what i have so far:

from __future__ import division
from pylab import *
import math as m
from numpy import *

array1=[]
nMax=1001
n=1
while n<nMax:
    array1.append(random.randint(1, 101))
    n=n+1
array1=array(array1)
array1=array1.reshape(1000,1)
print array1

array2=[]
nMax=1001
n=1
while n<nMax:
    array2.append(random.randint(1, 101))
    n=n+1
array2=array(array2)
array2=array2.reshape(1000,1)
print array2

This produces two single column arrays of 1000 random integers. One array is my random set of x coordinates and the other array my random set of y coordinates. I am trying to merge the two arrays so that i have one array of x and y coordinates, how do i do this? Once i have a single array of random x and y coordinates will i be able to use if, else statements to complete the question? e.g. if xcoordinate>85 or xcoordinate<15 or ycoordinate>85 or ycoordinate<15 then append a number to an empty list, then use the length of the list as a counter?

Please can someone give me some guidance on this question? Am i doing it the right way? If so how am i not? If so, how do i continue?

. . . Thanks for everyones help.

I have now managed to complete these two parts, but now i have been challenged to do this as an extension to the previous:

iii) Calculate how often one of your random locations falls within (either side) 15 pixels of a circle with a radius of 50 pixels, and write the result to the screen e.g. On average N% of the locations in the array fall in this region [N being a number between 0 and 100] Note that coordinates in the corners are within 15 pixels of the circle, then they should be included in this (if they are are more than 15 pixels away, then they shouldn’t be).

iv) Add code that requests a location within your array from the user and then alerts them if that location falls that region, e.g. Warning: your chosen location falls near the edge of the circle.

How would i even go about this? I have no idea of even where to begin. Can anybody give me some pointers please? Thanks

Upvotes: 0

Views: 270

Answers (1)

Joel Cornett
Joel Cornett

Reputation: 24788

You can skip actually generating the array and go directly to the frequency calculation:

First of all, it should be noted that since both the x and y coordinates are completely random:

We have the rule:

P(A or B) = P(A) + P(B) - P(A and B)

So we can say that

P(X or Y within 15) = P(X within 15) + P(Y within 15) - P(X and Y within 15)

Since it's implied that X and Y are uniformly random from 0 to 99 (or from 1 to 100), we can assume that

P(X within 15) = P(Y within 15)

So the whole thing becomes

P(X or Y within 15) = 2P(X within 15) - P(X within 15)^2

Now, we just need to calculate P(X within 15). Observe that X can only take integer values, so the function f(x) = |X - 49.5| - 35 will be greater than zero for x = 0..14 and x = 85..99. (We're indexing from zero, but it's the equivalent to going from 1...100).

So, the code:

from random import randrange

N = 3000
P_x_within_15 = sum(abs(randrange(100) - 49.5) - 35 > 0 for _ in range(N)) / float(N)

print "Frequency of X or Y within 15 pixels of edge:",
print 2 * P_x_within_15 - P_x_within_15 ** 2

As you may have noticed, we skipped actually creating an array, and just added up the boolean (True/False with True = 1, False = 0) results of the random numbers as they were generated.

EDIT - Actually creating an array

To create an array of random coordinates for a 100 x 100 matrix:

Using numpy the code would be:

from random import randrange
N = 3000
coords_array = array([randrange(100) for _ in range(2 * N)]).reshape(N, 2)

Using pure python, it would be:

from random import randrange
N = 3000
coords_list = [(randrange(100), randrange(100)) for _ in range(N)]

A couple things to note here:

  1. In python, indices are sequenced from zero. randrange(100) will give you a number between 0 and 99, inclusive.

  2. N is simply the number of coordinates we wish to generate

  3. Terminology point: In python, the data structures represented by square brackets are lists, not arrays (even though they are equivalent to arrays in other programming languages). In your code above array1 is a python list, not an array (which is a different data structure). Counterpoint: numpy however, extensively uses arrays. In your code, when you do array(array1), you are, in fact, creating a numpy array.

  4. The stuff between the square brackets is called a list comprehension. It is semantically equivalent to:


coords_list = []
for _ in range(N):
    coords_list.append((randrange(100), randrange(100)))

Upvotes: 1

Related Questions