1. Create a randomcharacter.py file (to be used as a module file for testing or implement program invocation) as follows:
From random import Randint
# generate a random character between Ch1 and CH2 (generate CH1 characters to CH2 characters any character)
def getrandomcharacter (CH1,CH2):
Return Chr (Randint (Ord (CH1), Ord (CH2)))
# Generate a random lowercase letter (generates any lowercase characters)
Def getrandomlowercaseletter ():
Return Getrandomcharacter (' A ', ' Z ')
# Generate a random uppercase letter (generates any uppercase characters)
Def getrandomuppercaseletter ():
Return Getrandomcharacter (' A ', ' Z ')
# Generate a random digit character (generates any 0-9 numeric characters)
Def getrandomdigitcharacter ():
return Getrandomcharacter (' 0 ', ' 9 ')
# Generate a random character
Def getrandomassicharacter ():
Return Chr (Randint (0,127))
2. Test program testrandom.py, call Randomcharacter module,
Import Randomcharacter
From random import Randint
Number_of_chars = 175
Char_per_line = 25
For I in Range (Number_of_chars):
Print (Randomcharacter.getrandomlowercaseletter (), end= "")
if (i + 1)% Char_per_line = = 0:
Print ()
Test program function: Generate 6 lines per line 25 total of 175 lowercase characters.
This article is from the "Bean Daddy Blog" blog, please make sure to keep this source http://imlearner.blog.51cto.com/9615100/1970335
Python function (uppercase, lowercase, numeric) that randomly produces a specific type of character