python3生成随机数实例
程序员文章站
2022-12-13 13:26:43
本文实例讲述了python3生成随机数的方法。分享给大家供大家参考。具体实现方法如下:
该实例是根据一本书上看到过一个随机数的小程序,经过自己改动,变为了一个猜数字的小游...
本文实例讲述了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)
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程序设计有所帮助。
上一篇: 降权网站后该怎么快速恢复?
下一篇: 黑客在线之经典入侵技术总结 (三)