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的id()函数介绍
Feb 10 Python
跟老齐学Python之集合的关系
Sep 24 Python
Python中的左斜杠、右斜杠(正斜杠和反斜杠)
Aug 30 Python
python中WSGI是什么,Python应用WSGI详解
Nov 24 Python
django 按时间范围查询数据库实例代码
Feb 11 Python
python实现对指定输入的字符串逆序输出的6种方法
Apr 26 Python
python按修改时间顺序排列文件的实例代码
Jul 25 Python
python切片(获取一个子列表(数组))详解
Aug 09 Python
wxPython色环电阻计算器
Nov 18 Python
python 实现人和电脑猜拳的示例代码
Mar 02 Python
详解Python中Pyyaml模块的使用
Oct 08 Python
Python爬虫制作翻译程序的示例代码
Feb 22 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
改造一台复古桌面收音机
2021/03/02 无线电
PHP如何透过ODBC来存取数据库
2006/10/09 PHP
JS中encodeURIComponent函数用php解码的代码
2012/03/01 PHP
解析CI的AJAX分页 另类实现方法
2013/06/27 PHP
深入理解PHP变量的值类型和引用类型
2015/10/21 PHP
thinkphp整合系列之极验滑动验证码geetest功能
2019/06/18 PHP
JS面向对象编程之对象使用分析
2010/08/19 Javascript
Knockout visible绑定使用方法
2013/11/15 Javascript
轻松创建nodejs服务器(1):一个简单nodejs服务器例子
2014/12/18 NodeJs
JS实现弹性菜单效果代码
2015/09/07 Javascript
jQuery插件formValidator自定义函数扩展功能实例详解
2015/11/25 Javascript
基于JS实现Android,iOS一个手势动画效果
2016/04/27 Javascript
js简单时间比较的方法
2016/08/02 Javascript
BootStrap中的表单大全
2016/09/07 Javascript
十大热门的JavaScript框架和库
2017/03/21 Javascript
使用JSON格式提交数据到服务端的实例代码
2018/04/01 Javascript
vue css 引入asstes中的图片无法显示的四种解决方法
2020/03/16 Javascript
vue递归获取父元素的元素实例
2020/08/07 Javascript
js 将多个对象合并成一个对象 assign方法的实现
2020/09/24 Javascript
[46:00]DOTA2上海特级锦标赛主赛事日 - 2 胜者组第一轮#4EG VS Fnatic第一局
2016/03/03 DOTA
Python实现Linux下守护进程的编写方法
2014/08/22 Python
放弃 Python 转向 Go语言有人给出了 9 大理由
2017/10/20 Python
Python封装原理与实现方法详解
2018/08/28 Python
在PyTorch中Tensor的查找和筛选例子
2019/08/18 Python
python实现图片二值化及灰度处理方式
2019/12/07 Python
如何使用selenium和requests组合实现登录页面
2020/02/03 Python
tensorflow使用freeze_graph.py将ckpt转为pb文件的方法
2020/04/22 Python
基于Python爬取搜狐证券股票过程解析
2020/11/18 Python
基于HTML5的WebGL经典3D虚拟机房漫游动画
2017/11/15 HTML / CSS
游戏商店:Eneba
2020/04/25 全球购物
索桥的故事教学反思
2014/02/06 职场文书
怎么写自荐书范文
2014/02/12 职场文书
小学校长竞聘演讲稿
2014/05/16 职场文书
关于长城的导游词
2015/01/30 职场文书
个人年终总结开头
2015/03/06 职场文书
解决SpringBoot文件上传临时目录找不到的问题
2021/07/01 Java/Android