How to set to this only uppercase letters? Random choice - python

code = ''.join(random.choice(string.letters) for i in xrange(6))
How to set to this only uppercase letters and add "RF" before?
RF(6 random uppercase letter)
Example:
RFQXCZLN

Use string.ascii_uppercase instead:
>>> import random
>>> import string
>>> 'RF' + ''.join(random.choice(string.ascii_uppercase) for i in xrange(6))
'RFOCJTGN'
Using random.choice() will result in a random string that can have repeating characters:
>>> 'RF' + ''.join(random.choice(string.ascii_uppercase) for i in xrange(6))
'RFBYBXHM'
Note the two B characters. If you do not want characters to repeat, then uses random.sample() instead:
>>> 'RF' + ''.join(random.sample(string.ascii_uppercase, 6))
'RFEVKZHD'
and the 6 random characters are guaranteed not to contain repeats.

Use random.sample and string.ascii_uppercase
>>> "RF" + ''.join(random.sample(string.ascii_uppercase, 6))
'RFBGHZMW'

>>> def getCode(length = 10, char = string.ascii_uppercase):
return ''.join(random.choice( char) for x in range(length))
>>> import random
>>> import string
>>> getCode(6)
'ADKCGW'
If your want other chars and length
>>> getCode(5, "mychars")
'ahssh'
A run on codepade
EDIT
to add "RE" in front of random chars
>>> "RE" + getCode(6)
REIUXWUE

Related

Generate a list of random string of fixed length in python [duplicate]

This question already has answers here:
What's the best way to generate random strings of a specific length in Python?
(7 answers)
Closed 7 years ago.
I need to generate a list which will have random alphanumeric string of fixed length. It will be something like: list = ['ag5','5b9','c85'] I can make the list with random numbers, but I can't work out to make strings which will have both numbers and letters.The list will have fixed length(say 100 item). I am using python 3.
To obtain the set of alphanumeric characters you can combine constants from the string module. Then you can use random.choice() in a list comprehension to generate the list:
from random import choice
from string import ascii_lowercase, digits
chars = ascii_lowercase + digits
lst = [''.join(choice(chars) for _ in range(2)) for _ in range(100)]
print(lst)
Since you need every single string to have both a letter and a number, we need to do a little more work than usual. We can either keep generating random words and only accept the ones which satisfy our constraint, or we can build a collection of characters that satisfies the constraint by construction.
from random import choice, shuffle
from string import digits, ascii_lowercase
def gen_word(N, min_N_digits, min_N_lower):
choose_from = [digits]*min_N_dig + [ascii_lowercase]*min_N_low
choose_from.extend([digits + ascii_lowercase] * (N-min_N_low-min_N_dig))
chars = [choice(bet) for bet in choose_from]
shuffle(chars)
return ''.join(chars)
def gen_word_rejection(N, min_N_digits, min_N_lower):
bet = digits + ascii_lowercase
while True:
word = ''.join([choice(bet) for i in range(N)])
if (sum(c.isdigit() for c in word) >= min_N_digits and
sum(c.islower() for c in word) >= min_N_lower):
return word
which gives me
>>> [gen_word(3, 1, 1) for i in range(5)]
['mb6', 'g4b', 'y5g', '28p', 'ki2']
>>> [gen_word_rejection(3, 1, 1) for i in range(5)]
['y37', 'dr0', 'w1z', 'h2a', 'i6r']
All of which have at least one digit and at least one lowercase letter.
I would recommend the hashids with a minimum length of three and a custom salt if you would like:
codes = [hasher.encode(i) for i in range(100)] # assuming hasher = hashids.Hashids('', 3)
I think, you want something like below:
>>> import string
>>> import random
>>> L1 = []
>>> my_chars = string.ascii_lowercase + string.digits #list of alphanumeric characters
>>> while(len(L1)<100): #To keep iterating until len(L1)=100
s = random.sample(my_chars,3) #Pick a sample(list) of three characters len from chars list
if not all(t in string.digits for t in s) and not all(t in string.ascii_lowercase for t in s): #Check if these characters are neither only digits nor letters, only mix of both will be allowed
L1.append(''.join(s)) #concatenate the characters to form the string and add it to list L1
>>> len(L1)
100
>>> L1
['f3m', 'gw2', '9ua', 'm4r', 'fv5', 'jw1', 'd1b', 'lh1', 'i61', '53m', 'j6y', 'fg6', '90d', 'xz1', 'n9f', 'k6r', '31b', 'm8w', '8w1', 'h5q', 'h3d', 'ju2', 'q1i', '6ci', '07m', '40c', 's0h', 'q1p', 'u2o', 'r4g', '6gq', 'rj4', '08x', 'yr6', 'il7', '21w', 'v3q', 'kv9', 'e4i', 'g3o', 'r2p', 'nl7', 'k8h', 'by9', 'qd1', 't71', 'x8f', 'uq3', 'k2z', '5i7', '7pc', 'd68', '6n0', '81y', 'c34', 'la0', 'a0c', '1d9', '7oi', 'z7x', '8l9', '0te', 'e9b', '2yp', '17h', 'vm1', 'vm1', 'ow9', '1ma', 'y7q', '7wa', 'a6b', '9uo', '5t2', 'i40', 'ja1', '16v', '0fe', '6bc', 'ek3', 'th6', '26g', 'a9n', 'fo5', '3hg', '4wz', 'v6z', 'r7b', '9cr', 'a0s', '8yp', 'v0f', 'es4', '8do', 'd0e', 'o6z', 'x3q', 'qw3', 'gi0', '0eg']

Delete a certain number of zeros from right of a string

I'm trying to delete a certain number of zeros from right. For example:
"10101000000"
I want to remove 4 zeros... And get:
"1010100"
I tried to do string.rstrip("0") or string.strip("0") but this removes all the of zeros from right. How can I do that?
The question is not a duplicate because I can't use imports.
You can use a regex
>>> import re
>>> mystr = "10101000000"
>>> numzeros = 4
>>> mystr = re.sub("0{{{}}}$".format(numzeros), "", mystr)
>>> mystr
'1010100'
This will leave the string as is if it doesn't end in four zeros
You could also check and then slice
if mystr.endswith("0" * numzeros):
mystr = mystr[:-numzeros]
For a known number of zeros you can use slicing:
s = "10101000000"
zeros = 4
if s.endswith("0" * zeros):
s = s[:-zeros]
rstrip deletes all characters from the end that are in passed set of characters. You can delete trailing zeros like this:
s = s[:-4] if s[-4:] == "0"*4 else s
Here's my solution:
number = "10101000000"
def my_rstrip(number, char, count=4):
for x in range(count):
if number.endswith(char):
number = number[0:-1]
else:
break
return number
print my_rstrip(number, '0', 4)
>>> s[:-4]+s[-4:].replace('0000','')
Don't forget to convert to str
import re
a = 10101000000
re.sub("0000$","", str(a))
You try to split off the last 4 characters from the string by this way:
string[:-4]

Extract a number from string in python

I want to extract a number form a string like this in Python:
string1 = 154787xs.txt
I want to get 154787 from there. I am using this:
searchPattern = re.compile('\d\d\d\d\d\d(?=xs)')
m = searchPattern.search(string1)
number = m.group()
but I do not get the correct value. Also the number of digits could change...
What am I doing wrong?
Simply you could use the below pattern,
searchPattern = re.compile(r'\d+(?=xs)')
Explanation:
\d+ matches one or more numbers.
(?=xs) Lookahead asserts that the characters which are following the numbers must be xs
Code:
>>> import re
>>> searchPattern = re.compile(r'\d+(?=xs)')
>>> m = searchPattern.search(string1)
>>> m
<_sre.SRE_Match object at 0x7f6047f66370>
>>> number = m.group()
>>> number
'154787'
What do you mean when you say you do not get the right value?
Your code does successfully match the string '154787'.
Perhaps you want number to be an int? In that case use:
number = int(m.group())
By the way, the regex could be written as
searchPattern = re.compile('(\d+)xs')
m = searchPattern.search(string1)
if m:
number = int(m.group(1))

Locating items in a nested list and decoding a ciphercode in python [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to search nested list grid and give lettered coordinates in Python?
I currently have this nested list acting as a 6x6 grid:
grid = [["Z","9","G","Q","T","3"],
["Y","8","F","P","S","2"],
["X","7","E","O","R","1"],
["W","6","D","N","M","0"],
["V","5","C","L","K","U"],
["J","4","B","I","H","A"]]
The rows and columns are referred to as A,B,C,D,E,F rather than the usual 0,1,2,3,4,5. Therefore the letters need to be converted to numbers and then the item has to be found in the nested list.
For example if the user typed in: FE CC ED ED CD
This should locate the letters for HELLO and print out the word HELLO as this was the encrypted message.
Any help on how I manage this would be greatly appreciated.
def letter_to_number(letter):
return ord(letter.upper()) - ord('A')
This reason this works is that ord(x) returns the value of character x in whatever your character set is—and, whether that's ASCII, Latin-1, UTF-8, or something else, the letters all come in order.
You can see this by experimenting with it in the interactive interpreter:
>>> ord('A')
65
>>> ord('B')
66
>>> ord('F')
70
>>> ord('F') - ord('A')
5
You could use a mapping to turn letters to numbers. Such a mapping is easy to generate from the string.ascii_uppercase string:
import string
letter_to_index = {l: i for i, l in enumerate(string.ascii_uppercase)}
Using letter_to_index it is then trivial to turn a letter (e.g. E), into a number (4):
>>> letter_to_index['E']
4
Using a space-separated input, this becomes:
letters = []
for coordinate in code.split():
row, column = letter_to_index[coordinate[0]], letter_to_index[coordinate[1]]
letters.append(grid[row][column])
print ''.join(letters)
Other techniques use ord() to get the ASCII ordinal for a letter (subtract ord('A') to make it 0-based) or using .index() on string.ascii_uppercase (which would be slower).
Comparing their relative speed with the timeit module is the best way to figure out which one is the fastest technique (if you so care):
>>> import timeit
>>> def lookup_map(coordinate):
... return letter_to_index[coordinate[0]], letter_to_index[coordinate[1]]
...
>>> def lookup_ord(coordinate, orda=ord('A')):
... return ord(coordinate[0]) - orda, ord(coordinate[1]) - orda
...
>>> def lookup_index(coordinate, upper=string.ascii_uppercase):
... return upper.index(coordinate[0]), upper.index(coordinate[1])
...
>>> coords = [random.choice(string.ascii_uppercase[:6])+random.choice(string.ascii_uppercase[:6]) for _ in range(1000)]
>>> timeit.timeit('[lookup(c) for c in coords]', 'from __main__ import coords, lookup_map as lookup', number=10000)
3.883746862411499
>>> timeit.timeit('[lookup(c) for c in coords]', 'from __main__ import coords, lookup_ord as lookup', number=10000)
4.582481861114502
>>> timeit.timeit('[lookup(c) for c in coords]', 'from __main__ import coords, lookup_index as lookup', number=10000)
9.782031059265137
The dictionary (mapping) approach is fastest; using ord() is close behind (if you compute ord('A') only once), and using .index() takes almost three times as long.
>>> input_string = "FE CC ED ED CD"
# Convert input string into integers
# `ord()` converts 'A' to 65, 'B' to 66, etc
>>> pairs = [tuple(ord(letter) - ord('A') for letter in pair) for pair in input_string.split()]
[(5, 4), (2, 2), (4, 3), (4, 3), (2, 3)]
# Now just retrieve the tuples from the list of pairs
# And look up the index in the grid
>>> output = ''.join(grid[x][y] for x, y in pairs)
'HELLO'
To convert the characters to integers you could do this:
def char_to_num(char):
return ord(char)-ord('A')
To access the characters in your grid, you would do something like this (I assume the input is referred to as input):
word = ""
for position in input.upper():
word += grid[char_to_num(position[0])][char_to_num(position[1])]
index=input('Enter the index:')
index=list(index)
a=[ord(i)-ord('A') for i in index]
print(grid[a[0]][a[1]])

Random string generation with upper case letters and digits

How do I generate a string of size N, made of numbers and uppercase English letters such as:
6U1S75
4Z4UKK
U911K4
Answer in one line:
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
or even shorter starting with Python 3.6 using random.choices():
''.join(random.choices(string.ascii_uppercase + string.digits, k=N))
A cryptographically more secure version: see this post
''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N))
In details, with a clean function for further reuse:
>>> import string
>>> import random
>>> def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
... return ''.join(random.choice(chars) for _ in range(size))
...
>>> id_generator()
'G5G74W'
>>> id_generator(3, "6793YUIO")
'Y3U'
How does it work ?
We import string, a module that contains sequences of common ASCII characters, and random, a module that deals with random generation.
string.ascii_uppercase + string.digits just concatenates the list of characters representing uppercase ASCII chars and digits:
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> string.ascii_uppercase + string.digits
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
Then we use a list comprehension to create a list of 'n' elements:
>>> range(4) # range create a list of 'n' numbers
[0, 1, 2, 3]
>>> ['elem' for _ in range(4)] # we use range to create 4 times 'elem'
['elem', 'elem', 'elem', 'elem']
In the example above, we use [ to create the list, but we don't in the id_generator function so Python doesn't create the list in memory, but generates the elements on the fly, one by one (more about this here).
Instead of asking to create 'n' times the string elem, we will ask Python to create 'n' times a random character, picked from a sequence of characters:
>>> random.choice("abcde")
'a'
>>> random.choice("abcde")
'd'
>>> random.choice("abcde")
'b'
Therefore random.choice(chars) for _ in range(size) really is creating a sequence of size characters. Characters that are randomly picked from chars:
>>> [random.choice('abcde') for _ in range(3)]
['a', 'b', 'b']
>>> [random.choice('abcde') for _ in range(3)]
['e', 'b', 'e']
>>> [random.choice('abcde') for _ in range(3)]
['d', 'a', 'c']
Then we just join them with an empty string so the sequence becomes a string:
>>> ''.join(['a', 'b', 'b'])
'abb'
>>> [random.choice('abcde') for _ in range(3)]
['d', 'c', 'b']
>>> ''.join(random.choice('abcde') for _ in range(3))
'dac'
This Stack Overflow quesion is the current top Google result for "random string Python". The current top answer is:
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
This is an excellent method, but the PRNG in random is not cryptographically secure. I assume many people researching this question will want to generate random strings for encryption or passwords. You can do this securely by making a small change in the above code:
''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N))
Using random.SystemRandom() instead of just random uses /dev/urandom on *nix machines and CryptGenRandom() in Windows. These are cryptographically secure PRNGs. Using random.choice instead of random.SystemRandom().choice in an application that requires a secure PRNG could be potentially devastating, and given the popularity of this question, I bet that mistake has been made many times already.
If you're using python3.6 or above, you can use the new secrets module as mentioned in MSeifert's answer:
''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(N))
The module docs also discuss convenient ways to generate secure tokens and best practices.
Simply use Python's builtin uuid:
If UUIDs are okay for your purposes, use the built-in uuid package.
One Line Solution:
import uuid; uuid.uuid4().hex.upper()[0:6]
In Depth Version:
Example:
import uuid
uuid.uuid4() #uuid4 => full random uuid
# Outputs something like: UUID('0172fc9a-1dac-4414-b88d-6b9a6feb91ea')
If you need exactly your format (for example, "6U1S75"), you can do it like this:
import uuid
def my_random_string(string_length=10):
"""Returns a random string of length string_length."""
random = str(uuid.uuid4()) # Convert UUID format to a Python string.
random = random.upper() # Make all characters uppercase.
random = random.replace("-","") # Remove the UUID '-'.
return random[0:string_length] # Return the random string.
print(my_random_string(6)) # For example, D9E50C
A simpler, faster but slightly less random way is to use random.sample instead of choosing each letter separately, If n-repetitions are allowed, enlarge your random basis by n times e.g.
import random
import string
char_set = string.ascii_uppercase + string.digits
print ''.join(random.sample(char_set*6, 6))
Note:
random.sample prevents character reuse, multiplying the size of the character set makes multiple repetitions possible, but they are still less likely then they are in a pure random choice. If we go for a string of length 6, and we pick 'X' as the first character, in the choice example, the odds of getting 'X' for the second character are the same as the odds of getting 'X' as the first character. In the random.sample implementation, the odds of getting 'X' as any subsequent character are only 6/7 the chance of getting it as the first character
import uuid
lowercase_str = uuid.uuid4().hex
lowercase_str is a random value like 'cea8b32e00934aaea8c005a35d85a5c0'
uppercase_str = lowercase_str.upper()
uppercase_str is 'CEA8B32E00934AAEA8C005A35D85A5C0'
From Python 3.6 on you should use the secrets module if you need it to be cryptographically secure instead of the random module (otherwise this answer is identical to the one of #Ignacio Vazquez-Abrams):
from secrets import choice
import string
''.join([choice(string.ascii_uppercase + string.digits) for _ in range(N)])
One additional note: a list-comprehension is faster in the case of str.join than using a generator expression!
A faster, easier and more flexible way to do this is to use the strgen module (pip install StringGenerator).
Generate a 6-character random string with upper case letters and digits:
>>> from strgen import StringGenerator as SG
>>> SG("[\u\d]{6}").render()
u'YZI2CI'
Get a unique list:
>>> SG("[\l\d]{10}").render_list(5,unique=True)
[u'xqqtmi1pOk', u'zmkWdUr63O', u'PGaGcPHrX2', u'6RZiUbkk2i', u'j9eIeeWgEF']
Guarantee one "special" character in the string:
>>> SG("[\l\d]{10}&[\p]").render()
u'jaYI0bcPG*0'
A random HTML color:
>>> SG("#[\h]{6}").render()
u'#CEdFCa'
etc.
We need to be aware that this:
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
might not have a digit (or uppercase character) in it.
strgen is faster in developer-time than any of the above solutions. The solution from Ignacio is the fastest run-time performing and is the right answer using the Python Standard Library. But you will hardly ever use it in that form. You will want to use SystemRandom (or fallback if not available), make sure required character sets are represented, use unicode (or not), make sure successive invocations produce a unique string, use a subset of one of the string module character classes, etc. This all requires lots more code than in the answers provided. The various attempts to generalize a solution all have limitations that strgen solves with greater brevity and expressive power using a simple template language.
It's on PyPI:
pip install StringGenerator
Disclosure: I'm the author of the strgen module.
Based on another Stack Overflow answer, Most lightweight way to create a random string and a random hexadecimal number, a better version than the accepted answer would be:
('%06x' % random.randrange(16**6)).upper()
much faster.
I thought no one had answered this yet lol! But hey, here's my own go at it:
import random
def random_alphanumeric(limit):
#ascii alphabet of all alphanumerals
r = (range(48, 58) + range(65, 91) + range(97, 123))
random.shuffle(r)
return reduce(lambda i, s: i + chr(s), r[:random.randint(0, len(r))], "")
If you need a random string rather than a pseudo random one, you should use os.urandom as the source
from os import urandom
from itertools import islice, imap, repeat
import string
def rand_string(length=5):
chars = set(string.ascii_uppercase + string.digits)
char_gen = (c for c in imap(urandom, repeat(1)) if c in chars)
return ''.join(islice(char_gen, None, length))
This method is slightly faster, and slightly more annoying, than the random.choice() method Ignacio posted.
It takes advantage of the nature of pseudo-random algorithms, and banks on bitwise and and shift being faster than generating a new random number for each character.
# must be length 32 -- 5 bits -- the question didn't specify using the full set
# of uppercase letters ;)
_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'
def generate_with_randbits(size=32):
def chop(x):
while x:
yield x & 31
x = x >> 5
return ''.join(_ALPHABET[x] for x in chop(random.getrandbits(size * 5))).ljust(size, 'A')
...create a generator that takes out 5 bit numbers at a time 0..31 until none left
...join() the results of the generator on a random number with the right bits
With Timeit, for 32-character strings, the timing was:
[('generate_with_random_choice', 28.92901611328125),
('generate_with_randbits', 20.0293550491333)]
...but for 64 character strings, randbits loses out ;)
I would probably never use this approach in production code unless I really disliked my co-workers.
edit: updated to suit the question (uppercase and digits only), and use bitwise operators & and >> instead of % and //
Use Numpy's random.choice() function
import numpy as np
import string
if __name__ == '__main__':
length = 16
a = np.random.choice(list(string.ascii_uppercase + string.digits), length)
print(''.join(a))
Documentation is here http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.random.choice.html
I'd do it this way:
import random
from string import digits, ascii_uppercase
legals = digits + ascii_uppercase
def rand_string(length, char_set=legals):
output = ''
for _ in range(length): output += random.choice(char_set)
return output
Or just:
def rand_string(length, char_set=legals):
return ''.join( random.choice(char_set) for _ in range(length) )
Sometimes 0 (zero) & O (letter O) can be confusing. So I use
import uuid
uuid.uuid4().hex[:6].upper().replace('0','X').replace('O','Y')
>>> import string
>>> import random
the following logic still generates 6 character random sample
>>> print ''.join(random.sample((string.ascii_uppercase+string.digits),6))
JT7K3Q
No need to multiply by 6
>>> print ''.join(random.sample((string.ascii_uppercase+string.digits)*6,6))
TK82HK
I used this method to generate random string of length n from a -> z
import random
s = ''.join(random.choice([chr(i) for i in range(ord('a'),ord('z'))]) for _ in range(10))
Security Oriented Approach
Our recommendation for anything security related is to avoid "rolling you own" and to use the secrets module which is specifically vetted for security.
This is from the best practices section of the docs:
import string
import secrets
alphabet = string.ascii_letters + string.digits
password = ''.join(secrets.choice(alphabet) for i in range(8))
Since you specifically asked for uppercase letters, you can either substitute ascii_uppercase for ascii_letters, or just uppercase the password with:
password = password.upper()
Standard Approach Not Aiming for Security
The canonical approach to this problem (as specified) uses the choices() function in the random module:
>>> from random import choices
>>> from string import ascii_uppercase, digits
>>> population = ascii_uppercase + digits
>>> str.join('', choices(population, k=6))
'6JWF1H'
>>> import random
>>> str = []
>>> chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
>>> num = int(raw_input('How long do you want the string to be? '))
How long do you want the string to be? 10
>>> for k in range(1, num+1):
... str.append(random.choice(chars))
...
>>> str = "".join(str)
>>> str
'tm2JUQ04CK'
The random.choice function picks a random entry in a list. You also create a list so that you can append the character in the for statement. At the end str is ['t', 'm', '2', 'J', 'U', 'Q', '0', '4', 'C', 'K'], but the str = "".join(str) takes care of that, leaving you with 'tm2JUQ04CK'.
Hope this helps!
For those of you who enjoy functional python:
from itertools import imap, starmap, islice, repeat
from functools import partial
from string import letters, digits, join
from random import choice
join_chars = partial(join, sep='')
identity = lambda o: o
def irand_seqs(symbols=join_chars((letters, digits)), length=6, join=join_chars, select=choice, breakup=islice):
""" Generates an indefinite sequence of joined random symbols each of a specific length
:param symbols: symbols to select,
[defaults to string.letters + string.digits, digits 0 - 9, lower and upper case English letters.]
:param length: the length of each sequence,
[defaults to 6]
:param join: method used to join selected symbol,
[defaults to ''.join generating a string.]
:param select: method used to select a random element from the giving population.
[defaults to random.choice, which selects a single element randomly]
:return: indefinite iterator generating random sequences of giving [:param length]
>>> from tools import irand_seqs
>>> strings = irand_seqs()
>>> a = next(strings)
>>> assert isinstance(a, (str, unicode))
>>> assert len(a) == 6
>>> assert next(strings) != next(strings)
"""
return imap(join, starmap(breakup, repeat((imap(select, repeat(symbols)), None, length))))
It generates an indefinite [infinite] iterator, of joined random sequences, by first generating an indefinite sequence of randomly selected symbol from the giving pool, then breaking this sequence into length parts which is then joined, it should work with any sequence that supports getitem, by default it simply generates a random sequence of alpha numeric letters, though you can easily modify to generate other things:
for example to generate random tuples of digits:
>>> irand_tuples = irand_seqs(xrange(10), join=tuple)
>>> next(irand_tuples)
(0, 5, 5, 7, 2, 8)
>>> next(irand_tuples)
(3, 2, 2, 0, 3, 1)
if you don't want to use next for generation you can simply make it callable:
>>> irand_tuples = irand_seqs(xrange(10), join=tuple)
>>> make_rand_tuples = partial(next, irand_tuples)
>>> make_rand_tuples()
(1, 6, 2, 8, 1, 9)
if you want to generate the sequence on the fly simply set join to identity.
>>> irand_tuples = irand_seqs(xrange(10), join=identity)
>>> selections = next(irand_tuples)
>>> next(selections)
8
>>> list(selections)
[6, 3, 8, 2, 2]
As others have mentioned if you need more security then set the appropriate select function:
>>> from random import SystemRandom
>>> rand_strs = irand_seqs(select=SystemRandom().choice)
'QsaDxQ'
the default selector is choice which may select the same symbol multiple times for each chunk, if instead you'd want the same member selected at most once for each chunk then, one possible usage:
>>> from random import sample
>>> irand_samples = irand_seqs(xrange(10), length=1, join=next, select=lambda pool: sample(pool, 6))
>>> next(irand_samples)
[0, 9, 2, 3, 1, 6]
we use sample as our selector, to do the complete selection, so the chunks are actually length 1, and to join we simply call next which fetches the next completely generated chunk, granted this example seems a bit cumbersome and it is ...
(1) This will give you all caps and numbers:
import string, random
passkey=''
for x in range(8):
if random.choice([1,2]) == 1:
passkey += passkey.join(random.choice(string.ascii_uppercase))
else:
passkey += passkey.join(random.choice(string.digits))
print passkey
(2) If you later want to include lowercase letters in your key, then this will also work:
import string, random
passkey=''
for x in range(8):
if random.choice([1,2]) == 1:
passkey += passkey.join(random.choice(string.ascii_letters))
else:
passkey += passkey.join(random.choice(string.digits))
print passkey
this is a take on Anurag Uniyal 's response and something that i was working on myself.
import random
import string
oneFile = open('‪Numbers.txt', 'w')
userInput = 0
key_count = 0
value_count = 0
chars = string.ascii_uppercase + string.digits + string.punctuation
for userInput in range(int(input('How many 12 digit keys do you want?'))):
while key_count <= userInput:
key_count += 1
number = random.randint(1, 999)
key = number
text = str(key) + ": " + str(''.join(random.sample(chars*6, 12)))
oneFile.write(text + "\n")
oneFile.close()
import string
from random import *
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(choice(characters) for x in range(randint(8, 16)))
print password
import random
q=2
o=1
list =[r'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','s','0','1','2','3','4','5','6','7','8','9','0']
while(q>o):
print("")
for i in range(1,128):
x=random.choice(list)
print(x,end="")
Here length of string can be changed in for loop i.e for i in range(1,length)
It is simple algorithm which is easy to understand. it uses list so you can discard characters that you do not need.
I was looking at the different answers and took time to read the documentation of secrets
The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.
In particularly, secrets should be used in preference to the default pseudo-random number generator in the random module, which is designed for modelling and simulation, not security or cryptography.
Looking more into what it has to offer I found a very handy function if you want to mimic an ID like Google Drive IDs:
secrets.token_urlsafe([nbytes=None])
Return a random URL-safe text string, containing nbytes random bytes. The text is Base64 encoded, so on average each byte results in approximately 1.3 characters. If nbytes is None or not supplied, a reasonable default is used.
Use it the following way:
import secrets
import math
def id_generator():
id = secrets.token_urlsafe(math.floor(32 / 1.3))
return id
print(id_generator())
Output a 32 characters length id:
joXR8dYbBDAHpVs5ci6iD-oIgPhkeQFk
I know this is slightly different from the OP's question but I expect that it would still be helpful to many who were looking for the same use-case that I was looking for.
A simple one:
import string
import random
character = string.lowercase + string.uppercase + string.digits + string.punctuation
char_len = len(character)
# you can specify your password length here
pass_len = random.randint(10,20)
password = ''
for x in range(pass_len):
password = password + character[random.randint(0,char_len-1)]
print password
I would like to suggest you next option:
import crypt
n = 10
crypt.crypt("any sring").replace('/', '').replace('.', '').upper()[-n:-1]
Paranoic mode:
import uuid
import crypt
n = 10
crypt.crypt(str(uuid.uuid4())).replace('/', '').replace('.', '').upper()[-n:-1]
Two methods :
import random, math
def randStr_1(chars:str, length:int) -> str:
chars *= math.ceil(length / len(chars))
chars = letters[0:length]
chars = list(chars)
random.shuffle(characters)
return ''.join(chars)
def randStr_2(chars:str, length:int) -> str:
return ''.join(random.choice(chars) for i in range(chars))
Benchmark :
from timeit import timeit
setup = """
import os, subprocess, time, string, random, math
def randStr_1(letters:str, length:int) -> str:
letters *= math.ceil(length / len(letters))
letters = letters[0:length]
letters = list(letters)
random.shuffle(letters)
return ''.join(letters)
def randStr_2(letters:str, length:int) -> str:
return ''.join(random.choice(letters) for i in range(length))
"""
print('Method 1 vs Method 2', ', run 10 times each.')
for length in [100,1000,10000,50000,100000,500000,1000000]:
print(length, 'characters:')
eff1 = timeit("randStr_1(string.ascii_letters, {})".format(length), setup=setup, number=10)
eff2 = timeit("randStr_2(string.ascii_letters, {})".format(length), setup=setup, number=10)
print('\t{}s : {}s'.format(round(eff1, 6), round(eff2, 6)))
print('\tratio = {} : {}\n'.format(eff1/eff1, round(eff2/eff1, 2)))
Output :
Method 1 vs Method 2 , run 10 times each.
100 characters:
0.001411s : 0.00179s
ratio = 1.0 : 1.27
1000 characters:
0.013857s : 0.017603s
ratio = 1.0 : 1.27
10000 characters:
0.13426s : 0.151169s
ratio = 1.0 : 1.13
50000 characters:
0.709403s : 0.855136s
ratio = 1.0 : 1.21
100000 characters:
1.360735s : 1.674584s
ratio = 1.0 : 1.23
500000 characters:
6.754923s : 7.160508s
ratio = 1.0 : 1.06
1000000 characters:
11.232965s : 14.223914s
ratio = 1.0 : 1.27
The performance of first method is better.
Generate random 16-byte ID containig letters, digits, '_' and '-'
os.urandom(16).translate((f'{string.ascii_letters}{string.digits}-_'*4).encode('ascii'))
import string, random
lower = string.ascii_lowercase
upper = string.ascii_uppercase
digits = string.digits
special = '!"£$%^&*.,##/?'
def rand_pass(l=4, u=4, d=4, s=4):
p = []
[p.append(random.choice(lower)) for x in range(l)]
[p.append(random.choice(upper)) for x in range(u)]
[p.append(random.choice(digits)) for x in range(d)]
[p.append(random.choice(special)) for x in range(s)]
random.shuffle(p)
return "".join(p)
print(rand_pass())
# #5U,#A4yIZvnp%51
If you want an easy-to-use but highly customisable key generator, use key-generator pypi package.
Here is the GitHub repo where you can find the complete documentation.
You can customise it to give a string jist like you want with many more options. Here's an example:
from key_generator.key_generator import generate
custom_key = generate(2, ['-', ':'], 3, 10, type_of_value = 'char', capital = 'mix', seed = 17).get_key()
print(custom_key) # ZLFdHXIUe-ekwJCu
Hope this helps :)
Disclaimer: This uses the key-generator library which I made.

Categories

Resources