仅在两个字母之间随机

时间:2015-08-04 18:39:38

标签: python

**

大家好,我每天都会游泳,这是我第一次问...我写了这段代码,不知道如何随机输入M或F的性别输出..这是我的代码:

**

from os import linesep
from random import Random

def generaterandomrecord(firstnamelength, lastnamelength, gender):
    rnd = Random()
    generatedline = ""


    generatedline += chr(rnd.randint(65, 90))
    for index in range(0, firstnamelength):
        randomasciinumber = rnd.randint(97, 122)
        generatedline += chr(randomasciinumber)

    generatedline += " "

    generatedline += chr(rnd.randint(65, 90))
    for index in range(0, lastnamelength):
        randomasciinumber = rnd.randint(97, 122)
        generatedline += chr(randomasciinumber)

    generatedline += ","

    for index in range(0, 10):
        generatedline += str(rnd.randint(0, 10) )

    generatedline += ","

    for index in range(0, gender):
        generatedline += chr(rnd.randint(106, 115))


    return generatedline

filepath = r"t:\test.txt"

filestream = open(filepath, "w")


rnd = Random()
for index in range(0, 300):
    firstnamelength = rnd.randint(3, 5)
    lastnamelength = rnd.randint(5,7 )
    gender = rnd.randint(106, 115)
    filestream.write(generaterandomrecord(firstnamelength, lastnamelength, gender) + linesep)

filestream.flush()

filestream.close()
print("Done processing")

谢谢

1 个答案:

答案 0 :(得分:5)

import random
print random.choice("MF")