欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3生成隨機(jī)數(shù)實(shí)例

 更新時(shí)間:2014年10月20日 15:23:39   投稿:shichen2014  
這篇文章主要介紹了python3生成隨機(jī)數(shù)的用法,實(shí)例講述了基于Python的隨機(jī)數(shù)的小程序,需要的朋友可以參考下

本文實(shí)例講述了python3生成隨機(jī)數(shù)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

該實(shí)例是根據(jù)一本書上看到過一個(gè)隨機(jī)數(shù)的小程序,經(jīng)過自己改動(dòng),變?yōu)榱艘粋€(gè)猜數(shù)字的小游戲,現(xiàn)在在python3下重寫了一遍。

這是一個(gè)控制臺(tái)下的猜數(shù)程序,winxp+python3.2+eric5和IDLE測試通過,但直接用winxp的命令行運(yùn)行有問題,原因還未知,慢慢找。ubuntu+python3.1測試通過。

具體實(shí)現(xiàn)代碼如下:

復(fù)制代碼 代碼如下:
# -*- coding: utf-8 -*-
import Image,ImageDraw,ImageFont
import random
import math, string 
 
class RandomChar():
  """用于隨機(jī)生成漢字"""
  @staticmethod
  def Unicode():
    val = random.randint(0x4E00, 0x9FBF)
    return unichr(val) 
 
  @staticmethod
  def GB2312():
    head = random.randint(0xB0, 0xCF)
    body = random.randint(0xA, 0xF)
    tail = random.randint(0, 0xF)
    val = ( head << 8 ) | (body << 4) | tail
    str = "%x" % val
    return str.decode('hex').decode('gb2312') 
 
class ImageChar():
  def __init__(self, fontColor = (0, 0, 0),
                     size = (100, 40),
                     fontPath = 'wqy.ttc',
                     bgColor = (255, 255, 255),
                     fontSize = 20):
    self.size = size
    self.fontPath = fontPath
    self.bgColor = bgColor
    self.fontSize = fontSize
    self.fontColor = fontColor
    self.font = ImageFont.truetype(self.fontPath, self.fontSize)
    self.image = Image.new('RGB', size, bgColor) 
 
  def rotate(self):
    self.image.rotate(random.randint(0, 30), expand=0) 
 
  def drawText(self, pos, txt, fill):
    draw = ImageDraw.Draw(self.image)
    draw.text(pos, txt, font=self.font, fill=fill)
    del draw 
 
  def randRGB(self):
    return (random.randint(0, 255),
           random.randint(0, 255),
           random.randint(0, 255)) 
 
  def randPoint(self):
    (width, height) = self.size
    return (random.randint(0, width), random.randint(0, height)) 
 
  def randLine(self, num):
    draw = ImageDraw.Draw(self.image)
    for i in range(0, num):
      draw.line([self.randPoint(), self.randPoint()], self.randRGB())
    del draw 
 
  def randChinese(self, num):
    gap = 5
    start = 0
    for i in range(0, num):
      char = RandomChar().GB2312()
      x = start + self.fontSize * i + random.randint(0, gap) + gap * i
      self.drawText((x, random.randint(-5, 5)), RandomChar().GB2312(), self.randRGB())
      self.rotate()
    self.randLine(18) 
 
  def save(self, path):
    self.image.save(path)

希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 一文詳解loss.item()用法和注意事項(xiàng)

    一文詳解loss.item()用法和注意事項(xiàng)

    loss.item()是PyTorch中的一種方法,用于計(jì)算損失函數(shù)的值,下面這篇文章主要給大家介紹了關(guān)于loss.item()用法和注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • TensorFlow數(shù)據(jù)輸入的方法示例

    TensorFlow數(shù)據(jù)輸入的方法示例

    這篇文章主要介紹了TensorFlow數(shù)據(jù)輸入的方法示例,主要介紹了4種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • pytorch 常用函數(shù) max ,eq說明

    pytorch 常用函數(shù) max ,eq說明

    這篇文章主要介紹了pytorch 常用函數(shù) max eq說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Opencv對象追蹤的示例代碼

    Opencv對象追蹤的示例代碼

    這篇文章主要介紹了Opencv對象追蹤的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Python魔法方法功能與用法簡介

    Python魔法方法功能與用法簡介

    這篇文章主要介紹了Python魔法方法功能與用法,結(jié)合具體實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中魔法方法的概念、功能、原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-04-04
  • python 匹配url中是否存在IP地址的方法

    python 匹配url中是否存在IP地址的方法

    今天小編就為大家分享一篇python 匹配url中是否存在IP地址的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python使用VIF實(shí)現(xiàn)檢測多重共線性

    Python使用VIF實(shí)現(xiàn)檢測多重共線性

    多重共線性是指多元回歸模型中有兩個(gè)或兩個(gè)以上的自變量,它們之間具有高度的相關(guān)性,本文主要介紹了如何使用VIF實(shí)現(xiàn)檢測多重共線性,需要的可以參考下
    2023-12-12
  • django2用iframe標(biāo)簽完成網(wǎng)頁內(nèi)嵌播放b站視頻功能

    django2用iframe標(biāo)簽完成網(wǎng)頁內(nèi)嵌播放b站視頻功能

    這篇文章主要介紹了django2 用iframe標(biāo)簽完成 網(wǎng)頁內(nèi)嵌播放b站視頻功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • 小學(xué)生也能看懂的python語法之循環(huán)語句精解

    小學(xué)生也能看懂的python語法之循環(huán)語句精解

    這篇文章主要介紹了詳解Python中的條件,循環(huán)語句,包括while循環(huán)for循環(huán),循環(huán)語句是學(xué)習(xí)各個(gè)編程語言的最基本的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2021-09-09
  • python index() 與 rindex() 方法的使用示例詳解

    python index() 與 rindex() 方法的使用示例詳解

    這篇文章主要介紹了python index() 與 rindex() 方法的使用,需要的朋友可以參考下
    2022-12-12

最新評論