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统计一个文本中重复行数的方法
Nov 19 Python
python实现同时给多个变量赋值的方法
Apr 30 Python
python利用不到一百行代码实现一个小siri
Mar 02 Python
Python解惑之整数比较详解
Apr 24 Python
python2与python3中关于对NaN类型数据的判断和转换方法
Oct 30 Python
深入解析Python小白学习【操作列表】
Mar 23 Python
python使用 zip 同时迭代多个序列示例
Jul 06 Python
django基于存储在前端的token用户认证解析
Aug 06 Python
与Django结合利用模型对上传图片预测的实例详解
Aug 07 Python
keras K.function获取某层的输出操作
Jun 29 Python
Python Pandas数据分析工具用法实例
Nov 05 Python
python基础详解之if循环语句
Apr 24 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
关于PHPDocument 代码注释规范的总结
2013/06/25 PHP
php实现微信和支付宝支付的示例代码
2020/08/11 PHP
SWFObject Flash js调用类
2008/07/08 Javascript
用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)
2012/06/22 Javascript
jquery的ajax()函数传值中文乱码解决方法介绍
2012/11/08 Javascript
JS实现匀速运动的代码实例
2013/11/29 Javascript
js 获取、清空input type=&quot;file&quot;的值(示例代码)
2013/12/24 Javascript
原生javascript实现图片弹窗交互效果
2015/01/12 Javascript
JavaScript通过setTimeout实时显示当前时间的方法
2015/04/16 Javascript
JavaScript框架是什么?怎样才能叫做框架?
2015/07/01 Javascript
JQuery操作textarea,input,select,checkbox方法
2015/09/02 Javascript
JavaScript解八皇后问题的方法总结
2016/06/12 Javascript
将List对象列表转换成JSON格式的类实现方法
2016/07/04 Javascript
深入浅析AngularJS中的一次性数据绑定 (bindonce)
2017/05/11 Javascript
Angular排序实例详解
2017/06/28 Javascript
jQuery实现可编辑表格并生成json结果(实例代码)
2017/07/19 jQuery
javascript帧动画(实例讲解)
2017/09/02 Javascript
JavaScript实现HTML5游戏断线自动重连的方法
2017/09/18 Javascript
vue路由事件beforeRouteLeave及组件内定时器的清除方法
2018/09/29 Javascript
vue安装遇到的5个报错及解决方法
2019/06/12 Javascript
Python之父谈Python的未来形式
2016/07/01 Python
Python通过matplotlib画双层饼图及环形图简单示例
2017/12/15 Python
Python使用paramiko操作linux的方法讲解
2019/02/25 Python
Python基本数据结构与用法详解【列表、元组、集合、字典】
2019/03/23 Python
python调用接口的4种方式代码实例
2019/11/19 Python
Python实现区域填充的示例代码
2021/02/03 Python
澳大利亚便宜的家庭购物网站:CrazySales
2018/02/06 全球购物
数百万免费的图形资源:Freepik
2020/09/21 全球购物
25道Java面试题集合
2013/05/21 面试题
物流经理自我评价
2013/09/23 职场文书
公司年底活动方案
2014/08/17 职场文书
领导干部整治奢华浪费之风思想汇报
2014/10/07 职场文书
研究生毕业论文导师评语
2014/12/31 职场文书
情人节活动总结范文
2015/02/05 职场文书
青春雷锋观后感
2015/06/10 职场文书
SQL Server的存储过程与触发器以及系统函数和自定义函数
2022/04/10 SQL Server