I need to make sequence of random strings, which increase(decrease) for alphabetic oder. For example: "ajikfk45kJDk", "bFJIPH7CDd", "c".
-
Your example strings do not strictly increase or decrease in any collating sequence or alphabetical order I know. Please clarify. – Prune Jul 26 '18 at 22:12
-
You need to have 26 strings where the first character is the letter of the alphabet respective to its position in the generated sequence, and the remaining characters are random? Why not just generate 26 random strings of `required_length - 1` and prepend the letters of the alphabets to them? – Sean Pianka Jul 26 '18 at 22:12
-
Possible duplicate of [Random string generation with upper case letters and digits in Python](https://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits-in-python) – joel Jul 26 '18 at 22:31
4 Answers
The simplest thing to do is to create N random strings and then sort them.
So, how do you create a random string? Well, you haven't specified what your rule is, but your three examples are strings of 1 to 12 characters taken from the set of ASCII lowercase, uppercase, and digits, so let's do that.
length = random.randrange(1, 13)
letters = random.choices(string.ascii_letters + string.digits, k=length)
string = ''.join(letters)
So, just do this N times, then sort it.
Putting it together:
chars = string.ascii_letters + string.digits
def make_string():
return ''.join(random.choices(chars, k=random.randrange(1, 13)))
def make_n_strings(n):
return sorted(make_string() for _ in range(n))
This should be simple enough that you can customize it however you want. Want case-insensitive sorting? Just add key=str.upper
to the sorted
. Want some other distribution of lengths? Just replace the randrange
. And so on.

- 354,177
- 51
- 601
- 671
You can use the chr() Python 3 function in a loop while generating random number in the ASCII category range you want.
You can find all the ASCII categories here or on Wikipedia.
For exemple :
chr(99)
-> equals c
More information about the chr() function on Python 3 official documentation.

- 85
- 1
- 12
The simplest way I can think of is
from random import randint
a = ''.join(sorted([chr(randint(33,127)) for i in range(randint(1,20))], reverse = False))
print(a)
reverse = True
makes it descending
-
1or you can use `random.choices(string.ascii_letters + string.digits, k=N)` for a random length N string, from https://stackoverflow.com/a/2257449/5986907 – joel Jul 26 '18 at 22:30
There's a lot of ways to do that, and this an easy and simple example to do that in Python 3 using Ascii char codes:-
from random import randint
def generateString(minLength, maxLength):
result = "";
resultLength = randint(minLength, maxLength)
for i in range(resultLength):
charType = randint(1,3)
if(charType == 1):
#number
result += chr(randint(48, 57))
elif(charType == 2):
#upper letter
result += chr(randint(65, 90))
elif(charType == 3):
#lower letter
result += chr(randint(97, 122))
return result;
#Example
print(generateString(1,20))

- 704
- 3
- 16