資源簡介 (共25張PPT)隨機點分布02CONTENTS1實例演示problems問題引入演示實例中在屏幕上畫的點有規(guī)律么沒有規(guī)律,再屏幕上畫點的大 小和位置都沒有規(guī)律,但是不 會畫出到屏幕外。02CONTENTS2思維構建constructs of thought思維建構引入隨機庫隨機分布范圍設置N個隨機點隨機設置大小02CONTENTS3編學邊練Computer English計算機英語randintrange范圍random隨機int整型random隨機庫工具 和海龜工具是不同的, 所以要導入random;引入隨機庫1import random as r隨機庫名稱1什么是隨機隨機到底是什么呢? 又有什么特點呢?像我們生活之中的拋硬 幣、擲骰子、猜丁殼都 是隨機事件;但是隨機也是有范圍的, 像擲6點骰子不能擲出 100點。random模塊也叫”隨機模塊”,以下是random模塊常用方法方法 格式 說明randint randint(開始數(shù)a,結束數(shù)b) 隨機生成一個a至b之間的整數(shù)choice choice(數(shù)據(jù)列) 隨機從數(shù)據(jù)列表中抽取一個數(shù)據(jù)shuffle shuffle(數(shù)據(jù)列) 隨機打亂數(shù)據(jù)列表中的數(shù)據(jù)的順序2生成隨機大小的點2生成隨機大小的點代碼隨機出來的數(shù),取得的隨機數(shù)是整數(shù)d = r.randint (1,50 )整型隨機大小范圍放隨機數(shù)的變量2生成隨機大小的點t . d o t ( 30 )畫點點的大小,數(shù)值表示直徑3隨機分布x坐標:x = r.randint(-200,200)x 坐標范圍y = r.randint(-150,150)y坐標:y 坐標范圍1循環(huán)輸出多點for i i n r a nge ( 10 ) :t.dot(6)循環(huán)次數(shù)循環(huán)內的語句要空四格(按一次tab鍵即可) range(10):循環(huán)10次,表示從0到10,不包含10 range(1,10):循環(huán)9次,表示從1到10,不包含1002CONTENTS4小 碼 當 家1隨機畫點要求:利用隨機數(shù),for循環(huán),結 合畫點方法,在屏幕上隨機 畫點;1參考代碼import turtle as timport random as rfor i in range(10):d = r.randint(1,50)x = r.randint(-200,200)y=r.randint(-150,150)t.penup()t.goto(x,y) t.pendown() t.dot(d)import turtle as timport randomt.speed(0)t.delay(0)t.bgcolor("black")def draw_colorful_star(x, y):size = random.randint(1,3)t.penup()t.goto(x, y)t.pendown()star_colors = ["red", "orange", "yellow", "green", "blue"]t.begin_fill()for i in range(5):t.color(random.choice(star_colors)) # 隨機選擇一種顏色t.dot(size * 20)t.end_fill()for _ in range(50):draw_colorful_star(random.randint(-300, 300), random.randint(-200, 200))02CONTENTS5小 碼 秀 秀第 一 題實現(xiàn)循環(huán)十次,下列代碼正確的是(多選):A. for i in range (10)for i in range (1,10)for i in range (1,11)for i in range (11)第 二題隨機生成1到10之間的整數(shù),下列代碼正確的是:A. r.randint (10)B.r.randfloat (10)C. r.randfloat (1,10)D. r.randint (1,10)課 堂 總 結本次課所學英文單詞random隨機for 循環(huán)t.randint(n,m)在n到m之間隨機一個數(shù)本次課所學編程知識t.goto(x,y)移到(x,y)點for i in range( ):循環(huán)range 范圍int 整數(shù)課 后 作 業(yè)隨機滿天星要求:隨機生成五角星坐標范圍:x(-200,200),y(-150,150)import turtle as timport randomt.speed(0)t.delay(0)t.bgcolor("black")def draw_colorful_star(x, y):size = random.randint(1,3)t.penup()t.goto(x, y)t.pendown()star_colors = ["red", "orange", "yellow", "green", "blue"]t.begin_fill()for i in range(5):t.color(random.choice(star_colors)) # 隨機選擇一種顏色t.dot(size * 20)t.end_fill()for _ in range(50):draw_colorful_star(random.randint(-300, 300), random.randint(-200, 200))同 學 們 再 見 ! 展開更多...... 收起↑ 資源預覽 縮略圖、資源來源于二一教育資源庫