{"id":993,"date":"2021-10-06T00:58:18","date_gmt":"2021-10-06T07:58:18","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=993"},"modified":"2021-10-06T00:58:18","modified_gmt":"2021-10-06T07:58:18","slug":"177-random-snowflakes-using-python-turtle","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/177-random-snowflakes-using-python-turtle\/","title":{"rendered":"#177 Random snowflakes using python turtle"},"content":{"rendered":"<p>#177 Random snowflakes using python turtle<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport turtle\r\nfrom random import randint\r\n\r\ndef generate_random_colour():\r\n    # Generates an R,G,B values randomly in range 0 to 255\r\n    r = randint(0, 255)\r\n    g = randint(0, 255)\r\n    b = randint(0, 255)\r\n    return r, g, b\r\n\r\ndef setup_screen(title, background = 'white'):\r\n    print('Set up Screen')\r\n    turtle.title(title)\r\n    turtle.setup(640, 600)\r\n    turtle.hideturtle()\r\n    turtle.penup()\r\n    turtle.tracer(200)\r\n    # Set the background colour of the screen\r\n    turtle.bgcolor(background)\r\n\r\ndef draw_snowflake(size):\r\n    # Draw a picture of a snowflake\r\n    turtle.penup()\r\n    turtle.forward(10 * size)\r\n    turtle.left(45)\r\n    turtle.pendown()\r\n    turtle.color(generate_random_colour())\r\n\r\n    # Draw branch 8 times to make a snowflake\r\n    for _ in range(8):\r\n        draw_branch(size)\r\n        turtle.forward(size)\r\n        turtle.left(45)\r\n\r\n    turtle.penup()\r\n\r\ndef draw_branch(size):\r\n    # Draw an individual branch on a snowflake\r\n    side_branch_size = size \/ 3\r\n    for _ in range(3):\r\n        for i in range(3):\r\n            turtle.forward(side_branch_size)\r\n            turtle.backward(side_branch_size)\r\n            turtle.right(45)\r\n        turtle.left(90)\r\n        turtle.backward(side_branch_size)\r\n        turtle.left(45)\r\n    turtle.right(90)\r\n\r\nsetup_screen('Snowflakes')\r\n# Indicates RGB numbers will be in the range 0 to 255\r\nturtle.colormode(255)\r\n\r\nprint(&quot;Drawing snowflakes at random locations&quot;)\r\nfor _ in range(500):\r\n    x = randint(-320, 100)\r\n    y = randint(-320, 80)\r\n    snowflake_size = randint(1, 2) * 10\r\n    turtle.goto(x, y)\r\n    turtle.right(15)\r\n    draw_snowflake(snowflake_size)\r\n\r\nturtle.update()\r\nprint('Done')\r\nturtle.done()\r\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex177.png?resize=642%2C632&#038;ssl=1\" alt=\"\" width=\"642\" height=\"632\" class=\"alignnone size-full wp-image-994\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex177.png 642w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex177-480x473.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 642px, 100vw\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>#177 Random snowflakes using python turtle import turtle from random import randint def generate_random_colour(): # Generates an R,G,B values randomly in range 0 to 255 r = randint(0, 255) g = randint(0, 255) b = randint(0, 255) return r, g, b def setup_screen(title, background = &#8216;white&#8217;): print(&#8216;Set up Screen&#8217;) turtle.title(title) turtle.setup(640, 600) turtle.hideturtle() turtle.penup() turtle.tracer(200) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2,13],"tags":[],"class_list":["post-993","post","type-post","status-publish","format-standard","hentry","category-python","category-turtle"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-g1","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":987,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/175-turtle-graphics-in-python\/","url_meta":{"origin":993,"position":0},"title":"#175 Turtle graphics in python","author":"gantovnik","date":"2021-10-05","format":false,"excerpt":"#175 Turtle graphics in python [code language=\"python\"] import turtle from random import randint def get_input_angle(): angle = 199 return angle def generate_random_colour(): # Generates an R,G,B values randomly in range 0 to 255 r = randint(0, 255) g = randint(0, 255) b = randint(0, 255) return r, g, b print('Set\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex175.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex175.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex175.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":990,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/176-the-koch-snowflake-using-python-turtle\/","url_meta":{"origin":993,"position":1},"title":"#176 The Koch snowflake using python turtle","author":"gantovnik","date":"2021-10-05","format":false,"excerpt":"#176 The Koch snowflake using python turtle import turtle # Set up Constants ANGLES = [60, -120, 60, 0] SIZE_OF_SNOWFLAKE = 300 def get_input_depth(): depth = 9 return depth def setup_screen(title, background='white', screen_size_x=640, screen_size_y=320, tracer_size=800): print('Set up Screen') turtle.title(title) turtle.setup(screen_size_x, screen_size_y) turtle.hideturtle() turtle.penup() turtle.backward(240) # Batch drawing to the screen\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex176.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":996,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/178-fractal-tree-using-python-turtle\/","url_meta":{"origin":993,"position":2},"title":"#178 Fractal tree using python turtle","author":"gantovnik","date":"2021-10-06","format":false,"excerpt":"#178 Fractal tree using python turtle [code language=\"python\"] import turtle # Set up 'constants' # image size IMAGE_SIZE_X = 500 IMAGE_SIZE_Y = 500 # Determines depth of tree - try 2 and 1.25 as alternatives FACTOR = 1.45 def draw_tree(length, width=9): color = 'brown' if length < 1: return elif\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex178.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":349,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/circle-of-diamonds-using-turtle\/","url_meta":{"origin":993,"position":3},"title":"Circle of diamonds using turtle","author":"gantovnik","date":"2019-01-26","format":false,"excerpt":"[code language=\"python\"] import os import turtle os.chdir(r'D:\\projects\\wordpress\\ex56') os.getcwd() def draw_diamond(some_turtle): some_turtle.left(30) some_turtle.forward(200) some_turtle.right(60) some_turtle.forward(200) some_turtle.right(120) some_turtle.forward(200) some_turtle.right(60) some_turtle.forward(200) some_turtle.right(150) def draw_art(): window = turtle.Screen() window.bgcolor(\"white\") brad = turtle.Turtle() brad.color(\"blue\") brad.speed(0) # Draw a circle with 36 diamonds. We rotate each diamond by 10 degrees at a time. for i in\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example56.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example56.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example56.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example56.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":269,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/circle-of-squares-using-turtle\/","url_meta":{"origin":993,"position":4},"title":"Circle of squares using turtle","author":"gantovnik","date":"2019-01-20","format":false,"excerpt":"[code language=\"python\"] import os import turtle os.chdir(r'D:\\projects\\wordpress\\ex51') os.getcwd() def draw_square(some_turtle): for i in range (1,5): some_turtle.forward(200) some_turtle.right(90) def draw_art(): #Turtle Brad n=5 brad = turtle.Turtle() brad.speed(10) for i in range(1,int(360\/n)+1): draw_square(brad) brad.right(n) draw_art() turtle.mainloop() [\/code] \u00a0 \u00a0","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"figure51","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/figure51.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/figure51.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/figure51.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1891,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/362-create-n-strings-of-length-k-using-symbols-from-the-given-alphabet-using-python\/","url_meta":{"origin":993,"position":5},"title":"#362 Create n strings of length k using symbols from the given alphabet using python.","author":"gantovnik","date":"2023-07-16","format":false,"excerpt":"[code language=\"python\"] import random def random_char(y): alphabet = ['A', 'T', 'C', 'G'] return ''.join(random.choice(alphabet) for x in range(y)) n=20 k=10 for i in range(0,n): print(random_char(k)) [\/code] Output: [code language=\"python\"] TCTGTTCCAG GACTAATACA AGAACTAAGT CCTCACCATG GACTTCCAGG CTGAGGCTTG TGAGACAGAG GATCTACCGA TCAAACTAAT ATCCGATCTT GAGGCACTCC AACCGAATGT CTCACTGTTC GTCCCTGGGA CGTGGTCCAA AGCCCGATGA CCTTCTCTCA GGCCGCTCGT CACCGGAGTG GAAGTGACGA [\/code]","rel":"","context":"In &quot;bioinformatics&quot;","block_context":{"text":"bioinformatics","link":"https:\/\/gantovnik.com\/bio-tips\/category\/bioinformatics\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=993"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/993\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}