python3生成随机数实例


Posted in Python onOctober 20, 2014

本文实例讲述了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))  

 

  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程序设计有所帮助。

Python 相关文章推荐
巧用Python装饰器 免去调用父类构造函数的麻烦
May 18 Python
轻松理解Python 中的 descriptor
Sep 15 Python
python使用os.listdir和os.walk获得文件的路径的方法
Dec 16 Python
关于django 数据库迁移(migrate)应该知道的一些事
May 27 Python
对python3 一组数值的归一化处理方法详解
Jul 11 Python
Django生成PDF文档显示网页上以及PDF中文显示乱码的解决方法
Dec 17 Python
在Sublime Editor中配置Python环境的详细教程
May 03 Python
Python使用jupyter notebook查看ipynb文件过程解析
Jun 02 Python
Python在后台自动解压各种压缩文件的实现方法
Nov 10 Python
python实现代码审查自动回复消息
Feb 01 Python
pytorch中[..., 0]的用法说明
May 20 Python
在NumPy中深拷贝和浅拷贝相关操作的定义和背后的原理
Apr 14 Python
Python入门篇之面向对象
Oct 20 #Python
Python入门篇之数字
Oct 20 #Python
Python入门篇之正则表达式
Oct 20 #Python
Python入门篇之文件
Oct 20 #Python
Python入门篇之函数
Oct 20 #Python
Python入门篇之条件、循环
Oct 17 #Python
Python入门篇之字典
Oct 17 #Python
You might like
WordPress中注册菜单与调用菜单的方法详解
2015/12/18 PHP
jsPDF生成pdf后在网页展示实例
2014/01/16 Javascript
用js闭包的方法实现多点标注冒泡示例
2014/05/29 Javascript
火狐下input焦点无法重复获取问题的解决方法
2014/06/16 Javascript
jQuery中:header选择器用法实例
2014/12/29 Javascript
学习JavaScript设计模式之中介者模式
2016/01/14 Javascript
jQuery实现的导航下拉菜单效果示例
2016/09/05 Javascript
require.js+vue开发微信上传图片组件
2016/10/27 Javascript
详解JS中的attribute属性
2017/04/25 Javascript
使用jQuery实现页面定时弹出广告效果
2017/08/24 jQuery
JavaScript面向对象精要(上部)
2017/09/12 Javascript
微信小程序 动画的简单实例
2017/10/12 Javascript
详解webpack性能优化——DLL
2017/10/20 Javascript
JS无限级导航菜单实现方法
2019/01/05 Javascript
命令行批量截图Node脚本示例代码
2019/01/25 Javascript
node.js 基于cheerio的爬虫工具的实现(需要登录权限的爬虫工具)
2019/04/10 Javascript
vue+elementUi图片上传组件使用详解
2019/08/20 Javascript
[02:18]《我与DAC》之工作人员:为了热爱DOTA2的玩家们
2018/03/28 DOTA
[02:10]DOTA2 TI10勇士令状玩法及不朽Ⅰ展示:焕新世界,如你所期
2020/05/29 DOTA
Python实现的多项式拟合功能示例【基于matplotlib】
2018/05/15 Python
详解利用Python scipy.signal.filtfilt() 实现信号滤波
2019/06/05 Python
tensor和numpy的互相转换的实现示例
2019/08/02 Python
python模块hashlib(加密服务)知识点讲解
2019/11/25 Python
python爬虫模拟浏览器的两种方法实例分析
2019/12/09 Python
python logging添加filter教程
2019/12/24 Python
PyTorch里面的torch.nn.Parameter()详解
2020/01/03 Python
python GUI库图形界面开发之PyQt5动态加载QSS样式文件
2020/02/25 Python
Python selenium模块实现定位过程解析
2020/07/09 Python
美国嘻哈首饰购物网站:Hip Hop Bling
2016/12/30 全球购物
会计专业职业规划:规划自我赢取未来
2014/02/12 职场文书
《威尼斯的小艇》教学反思
2014/02/17 职场文书
哈弗商学院毕业生求职信
2014/02/26 职场文书
党的群众路线教育实践方案
2014/05/11 职场文书
2015年管理人员工作总结
2015/05/13 职场文书
2016年党员创先争优承诺书
2016/03/25 职场文书
java Nio使用NioSocket客户端与服务端交互实现方式
2021/06/15 Java/Android