|
1 | | -import random as rand |
2 | | - |
3 | | -def RandString(type, length): |
4 | | - length = int(length) |
5 | | - if length <= 0: raise Exception("Invalid length in RandString({}, {})".format(type, length)) |
6 | | - if type == "uppercase": |
7 | | - response = "" |
8 | | - for i in range(length): |
9 | | - code = rand.randint(65, 90) |
10 | | - response += chr(code) |
11 | | - return response |
12 | | - elif type == "lowercase": |
13 | | - response = "" |
14 | | - for i in range(length): |
15 | | - code = rand.randint(97, 122) |
16 | | - response += chr(code) |
17 | | - return response |
18 | | - elif type == "upperlower": |
19 | | - response = "" |
20 | | - choices = ["upper", "lower"] |
21 | | - code = 0 |
22 | | - for i in range(length): |
23 | | - choice = rand.choice(choices) |
24 | | - if choice == "upper": code = rand.randint(65, 90) |
25 | | - else: code = rand.randint(97, 122) |
26 | | - response += chr(code) |
27 | | - return response |
28 | | - elif type == "alphanumerical": |
29 | | - response = "" |
30 | | - choices = ["upper", "lower", "number"] |
31 | | - code = 0 |
32 | | - for i in range(length): |
33 | | - choice = rand.choice(choices) |
34 | | - if choice == "upper": code = rand.randint(65, 90) |
35 | | - elif choice == "lower": code = rand.randint(97, 122) |
36 | | - else: code = rand.randint(48, 57) |
37 | | - response += chr(code) |
38 | | - return response |
39 | | - elif type == "ascii": |
40 | | - response = "" |
41 | | - choice = 0 |
42 | | - code = 0 |
43 | | - for i in range(length): |
44 | | - choice = rand.randint(0, 1) |
45 | | - if choice == 0: code = rand.randint(33, 95) |
46 | | - else: code = rand.randint(97, 125) |
47 | | - response += chr(code) |
48 | | - return response |
49 | | - else: raise Exception("Invalid type in RandString({}, {})".format(type, length)) |
| 1 | +import random as rand |
| 2 | + |
| 3 | +def RandString(type, length): |
| 4 | + length = int(length) |
| 5 | + if length <= 0: raise Exception("Invalid length in RandString({}, {})".format(type, length)) |
| 6 | + if type == "uppercase": |
| 7 | + response = "" |
| 8 | + for i in range(length): |
| 9 | + code = rand.randint(65, 90) |
| 10 | + response += chr(code) |
| 11 | + return response |
| 12 | + elif type == "lowercase": |
| 13 | + response = "" |
| 14 | + for i in range(length): |
| 15 | + code = rand.randint(97, 122) |
| 16 | + response += chr(code) |
| 17 | + return response |
| 18 | + elif type == "upperlower": |
| 19 | + response = "" |
| 20 | + choices = ["upper", "lower"] |
| 21 | + code = 0 |
| 22 | + for i in range(length): |
| 23 | + choice = rand.choice(choices) |
| 24 | + if choice == "upper": code = rand.randint(65, 90) |
| 25 | + else: code = rand.randint(97, 122) |
| 26 | + response += chr(code) |
| 27 | + return response |
| 28 | + elif type == "alphanumerical": |
| 29 | + response = "" |
| 30 | + choices = ["upper", "lower", "number"] |
| 31 | + code = 0 |
| 32 | + for i in range(length): |
| 33 | + choice = rand.choice(choices) |
| 34 | + if choice == "upper": code = rand.randint(65, 90) |
| 35 | + elif choice == "lower": code = rand.randint(97, 122) |
| 36 | + else: code = rand.randint(48, 57) |
| 37 | + response += chr(code) |
| 38 | + return response |
| 39 | + elif type == "ascii": |
| 40 | + response = "" |
| 41 | + choice = 0 |
| 42 | + code = 0 |
| 43 | + for i in range(length): |
| 44 | + choice = rand.randint(0, 1) |
| 45 | + if choice == 0: code = rand.randint(33, 95) |
| 46 | + else: code = rand.randint(97, 125) |
| 47 | + response += chr(code) |
| 48 | + return response |
| 49 | + else: raise Exception("Invalid type in RandString({}, {})".format(type, length)) |
0 commit comments