国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python3生成隨機數實例

2020-02-23 06:06:09
字體:
來源:轉載
供稿:網友

本文實例講述了python3生成隨機數的方法。分享給大家供大家參考。具體實現方法如下:

該實例是根據一本書上看到過一個隨機數的小程序,經過自己改動,變為了一個猜數字的小游戲,現在在python3下重寫了一遍。

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

具體實現代碼如下:

代碼如下:# -*- coding: utf-8 -*-
import Image,ImageDraw,ImageFont
import random
import math, string 
 
class RandomChar():
  """用于隨機生成漢字"""
  @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)) 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玉屏| 明光市| 商丘市| 海门市| 辽中县| 上思县| 建平县| 阜康市| 梅河口市| 台中市| 商丘市| 虞城县| 兰溪市| 玛曲县| 桂阳县| 永顺县| 社旗县| 沭阳县| 包头市| 兰州市| 肥西县| 保定市| 高州市| 仪陇县| 邹城市| 焉耆| 同心县| 福鼎市| 江安县| 海晏县| 台江县| 台北县| 双牌县| 密山市| 阳泉市| 郴州市| 调兵山市| 东至县| 临泽县| 尚志市| 扎兰屯市|