Python Battleship random number generator
<p>I'm fairly new to classes in Python. While coding a battleship game I ran into a problem with choosing random x,y coordinates for the locations of computer's ships and the computer's attack coordinates. I am confused about whether to generate random numbers as a local variable in one of the functions or as Class attribute or instance attribute. </p>

<p>Initially I thought to create an instance attribute (below), but i got rand_x is not defined. I tried creating a Battleship function that generated random numbers, but it returned the same pair of coordinates every time it was called. Is the only way to do this to create a local variable for random #s? Because I will be using the random generator more than once it would be nice not to have to repeat that code.</p>

<p>Thanks for your patience.</p>

<p>EDIT: I've included more code with the randomness function, and replaced <code>size</code> in randomness function with <code>self.size</code>.  </p>

<p>For example, Battleship(4,2,0,0) might give me a <code>hitlist</code> of [[2,1],[2,1]] I would like random #s inside <code>hitlist</code>.</p>

<pre><code>import random
hitlist=[]; #a global variable

class Battleship(object):
    """ Ship object container. A game where the user tries to destroy the enemy's ships User tries to guess computer's position x and y """
    def __init__(self, size, numberships,position_x,position_y):
        self.position_x=position_x
        self.position_y=position_y
        self.numberships=numberships
        self.size = size

    def plotships(self,r):
        """input is integer coordinates for ships and output is an array of arrays with battleship locations CREATES THE HITLIST DONT REPEAT"""
        print('plotships function running')
        for i in range(self.numberships):
            hitlist.append(r) #random number from function randomness
            print(hitlist)
        return hitlist

   def randomness(self):
        rand_x=random.choice(range(self.size))
        rand_y=random.choice(range(self.size))
        randcoord=[rand_x,rand_y]
        return randcoord

#Game Interface
size=int(input('Gameboard size'))
numberships=int(input('Waiting for Number of enemy ships'))
b=Battleship(size,numberships,0,0)
random=b.randomness() #create a random x y coordinate
b.plotships(random) #create a hitlist
</code></pre>

<p>I think it's because you're calling <code>random.choice</code> with <code>size</code> and not <code>self.size</code>.</p>

<p>i.e.</p>

<pre><code>rand_x = random.choice(range(self.size))
</code></pre>

<p>Also, where are you defining <code>self.rand</code>? Surely you're getting problems in the constructor trying to print it?</p>

<p>EDIT - IN RESPONSE TO COMMENT BELOW:</p>

<p>If you want <code>hitlist</code> to be populated with <code>self.numberships</code> pairs of independent random number pairs, the write the <code>plotships</code> method as:</p>

<pre><code>def plotships(self):
    """input is integer coordinates for ships and output is an array of arrays with battleship locations CREATES THE HITLIST DONT REPEAT"""
    print('plotships function running')
    for i in range(self.numberships):
        hitlist.append(randomness()) #random number from function randomness
        print(hitlist)
    return hitlist
</code></pre>

python