Skip to content

Commit bed72f3

Browse files
authored
Add files via upload
1 parent 46e1fa8 commit bed72f3

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

rand-string/RandString.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
else: raise Exception("Invalid type in RandString({}, {})".format(type, length))
40+
print(RandString("alphanumerical", 10))

0 commit comments

Comments
 (0)