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 相关文章推荐
Django视图之ORM数据库查询操作API的实例
Oct 27 Python
Python内置函数——__import__ 的使用方法
Nov 24 Python
Python实现替换文件中指定内容的方法
Mar 19 Python
python中join()方法介绍
Oct 11 Python
对Python中创建进程的两种方式以及进程池详解
Jan 14 Python
python解压TAR文件至指定文件夹的实例
Jun 10 Python
Python文件路径名的操作方法
Oct 30 Python
Python3直接爬取图片URL并保存示例
Dec 18 Python
pytorch: Parameter 的数据结构实例
Dec 31 Python
python实现飞行棋游戏
Feb 05 Python
python中resample函数实现重采样和降采样代码
Feb 25 Python
Python smtp邮件发送模块用法教程
Jun 15 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
thinkphp5.1框架容器与依赖注入实例分析
2019/07/23 PHP
jquery选择器的选择使用及性能介绍
2013/01/16 Javascript
使用jQuery清空file文件域的解决方案
2013/04/12 Javascript
JavaScript获取onclick、onchange等事件值的代码
2013/07/22 Javascript
JavaScript中伪协议 javascript:使用探讨
2014/07/18 Javascript
jQuery循环动画与获取组件尺寸的方法
2015/02/02 Javascript
JavaScript测试工具之Karma-Jasmine的安装和使用详解
2015/12/03 Javascript
微信小程序中页面FOR循环和嵌套循环
2017/06/21 Javascript
Angular4表单验证代码详解
2017/09/03 Javascript
学习jQuery中的noConflict()用法
2018/09/28 jQuery
深入浅析javascript函数中with
2018/10/28 Javascript
详解Vue.js自定义tipOnce指令用法实例
2018/12/19 Javascript
Vue.js组件高级特性实例详解
2018/12/24 Javascript
vue实现简单的日历效果
2020/09/24 Javascript
简单介绍Python中的JSON模块
2015/04/08 Python
Python3.x中自定义比较函数
2015/04/24 Python
Python标准库defaultdict模块使用示例
2015/04/28 Python
利用numpy+matplotlib绘图的基本操作教程
2017/05/03 Python
在python中利用最小二乘拟合二次抛物线函数的方法
2018/12/29 Python
python模拟键盘输入 切换键盘布局过程解析
2019/08/15 Python
python禁用键鼠与提权代码实例
2019/08/16 Python
pygame实现俄罗斯方块游戏(AI篇2)
2019/10/29 Python
python给指定csv表格中的联系人群发邮件(带附件的邮件)
2019/12/31 Python
Tensorflow限制CPU个数实例
2020/02/06 Python
使用python实现CGI环境搭建过程解析
2020/04/28 Python
详解Html5 Canvas画线有毛边解决方法
2018/03/01 HTML / CSS
HTML5 File API改善网页上传功能
2009/08/19 HTML / CSS
Expedia挪威官网:酒店、机票和租车
2018/03/03 全球购物
我想声明一个指针并为它分配一些空间, 但却不行。这些代码有什么 问题?char *p; *p = malloc(10);
2016/10/06 面试题
发展部经理职责规定
2014/02/22 职场文书
社区文明倡议书
2015/04/28 职场文书
春季运动会加油词
2015/07/18 职场文书
用Python制作灯光秀短视频的思路详解
2021/04/13 Python
用python画城市轮播地图
2021/05/28 Python
基于python定位棋子位置及识别棋子颜色
2021/07/26 Python
JS前端使用canvas实现物体的点选示例
2022/08/05 Javascript