title: How to generate a random star using turtle graphics in Python?
body: <p>I'm new to turtle graphics in Python, and I'm running into some issues with a particular problem. I'm trying to generate a star that uses a <code>while</code> loop to draw random jagged lines from the center of a circle. </p>
  
  <p>Each line should have a distance of 250. I'm using the <code>penup</code> <code>pendown</code> and <code>setpos</code> commands within the loop to draw these random lines, and each line should be a random color.</p>
  
  <p>Here's an idea of what I'm hoping to generate: <a href="https://i.stack.imgur.com/b4cmi.png" rel="nofollow noreferrer">random star</a></p>
  
  <p>Here's the code I have so far:</p>
  
  <pre><code># tg_random_star.py
  
  from random import randrange
  from turtle import *
  
  MAX_ANGLE = 30
  
  def jaggedLine(turtle, pieces, pieceLength):
  
      for i in range(pieces):
          turtle.forward(pieceLength)
          r = randrange(-MAX_ANGLE, MAX_ANGLE + 1)
          turtle.right(r)
  
  def jumpToCenter(turtle):
      turtle.penup()
      turtle.setpos(0, 0)
      turtle.pendown()
  
  def randomColor(turtle):
      turtle.colormode(255)
      r = randrange(255)    # red component of color
      g = randrange(255)    # green component
      b = randrange(255)    # blue component
  
      turtle.pencolor(r, g, b)
  
  def main():
      colormode(255)
      t = Turtle()
      jaggedLine(t, 10, 30)
      jumpToCenter(t)
      jaggedLine(t, 10, 30)
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>It currently generates 2 lines, but the <code>turtle.pencolor(r, g, b)</code> and the <code>colormode(255)</code> don't seem to be working, as both lines are black. Any idea why these lines aren't in color?</p>
  
  <p>Rather than using <code>for i in range(pieces)</code> to draw lines that are based on the number of segments, how can I use a <code>while</code> loop to draw jagged lines that each have a distance of 250? In other words, I want each line to have a distance of 250 before drawing a new line from the center.</p>
  
  <p>(Maybe I could use the <code>xcor</code> and <code>ycor</code> methods to find the turtle’s position, then calculate the distance using the distance formula?)</p>
  
  <pre><code>def distance(p0, p1):
  return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)`
  </code></pre>
  
  <p>Any help or explanation would be appreciated, thank you.</p>