Python3 generates random number instances and python3 random number instances

Source: Internet
Author: User
Tags drawtext

Python3 generates random number instances and python3 random number instances

This example describes how to generate a random number using python3. Share it with you for your reference. The specific implementation method is as follows:

This example is based on a small random number program seen in a book. After modification, it is changed to a number-guessing game and is now rewritten under python3.

This is a guess program in the console. winxp + python3.2 + eric5 and IDLE passed the test, but there is a problem with running the command line of winxp. The reason is unknown. Ubuntu + python3.1 passed the test.

The specific implementation code is as follows:

Copy codeThe Code is as follows: #-*-coding: UTF-8 -*-
Import Image, ImageDraw, ImageFont
Import random
Import math, string
 
Class RandomChar ():
"Used to randomly generate Chinese characters """
@ 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 = (1, 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)

I hope this article will help you with Python programming.


How to Use python to generate a list composed of random numbers?

Need to use the random Module
Import random
N = random. randint (1, 10)
A random integer ranging from 1 to 10 is generated.
Insert list
[]. Append (n)
That's all. You can create a loop. For example, keep inserting the for loop.

Use python to randomly generate three arrays with ten elements and combine the three into an array. Pick out the odd and even numbers which are not repeated.

# Coding = UTF-8
'''
Created on 2012-6-4

@ Author: Administrator
'''
Import random

Def test ():
MinNum = 0 # random number start
MaxNum = 999999 # maximum Random Number
# Three sequences are randomly generated and are integers.
List1 = [random. randint (minNum, maxNum) for I in xrange (10)]
List2 = [random. randint (minNum, maxNum) for I in xrange (10)]
List3 = [random. randint (minNum, maxNum) for I in xrange (10)]
LastList = list1 + list2 + list3 # combine
LastList = list (set (lastList) # remove duplicates. The set is unordered and not repeated.

OddNumList = [] # used to save the odd Sequence
EvenNumList = [] # used to save even numbers

For num in lastList:
If num % 2 = 0:
EvenNumList. append (num)
Else:
OddNumList. append (num)
Print u ":", oddNumList, "% d" % len (oddNumList)
Print u "even number:", evenNumList, "% d total" % len (evenNumList)

If _ name _ = '_ main __':
Test ()
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.