![]() > my_chars = string.ascii_lowercase + string. I think, you want something like below: > import string Let's write a program to print a secure random string using the secrets.choice method. It is a cryptographically random string generator that ensures no two processes can obtain the same results simultaneously using secrets.choice() method. > Īll of which have at least one digit and at least one lowercase letter. A secrets.choice() method is used to generate a more secure random string than random.choice(). Sum(c.islower() for c in word) >= min_N_lower): If (sum(c.isdigit() for c in word) >= min_N_digits and from random import choice, shuffleįrom string import digits, ascii_lowercaseĭef gen_word(N, min_N_digits, min_N_lower):Ĭhoose_from = *min_N_dig + *min_N_lowĬhoose_from.extend( * (N-min_N_low-min_N_dig))Ĭhars = ĭef gen_word_rejection(N, min_N_digits, min_N_lower): 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. You could use str() in place of bytes(), but bytes() is better for Python 3, since Python 3 strings are Unicode.Since you need every single string to have both a letter and a number, we need to do a little more work than usual. Return bytes(bytearray(getrandbits(8) for _ in xrange(n))) Here's a variant based on cristianmtr's new answer: def random_bytes(n): Return ''.join()ĭepending on how you intend to use these random bytes, you may find it useful to put them into a bytearray or bytes object. We can streamline this and make it slightly more efficient by using a list comprehension: def random_bytes(n): We can also save a little bit of time by pre-generating a list of our 1 byte strings rather than calling chr() for every byte we want. ![]() ![]() def genRandData (size): buf chr (random.randint (0,255)) for i in range (size-1): buf buf + chr (random. I'm looking for a way to generate a random string of n bytes in Python in a similar way to os.urandom () method except providing a way to seed the data generation. The usual technique in Python for building a string from substrings is to accumulate the substrings in a list then to use the str.join() method to combine them in one go. Generating random string of seedable data. However, if you don't want to use numpy you can make your code a little more efficient.īuilding a string by concatenation eg buf = buf + chr(random.randint(0,255)) is very slow, since a new buf has to be allocated on every loop (remember, Python strings are immutable). The simplest random string Generator i know: import random, string allchars string.asciilowercase def randstr (length): result str () for i in range (length): result + random. says, letting numpy generate your bytes in one hit at C speed is going to be way faster than producing them one at a time at Python speed. Timings: In : %time generateRandomString(1000000)Īs Dan D. First, import the random module and define a string containing all possible characters that can be included in the random string. The os.urandom() function for details on availability).ĭef generateRandomString(size, seed=None): To generate a random string of a given length in Python, you can use the random module along with string operations. If randomness sources are providedīy the operating system, they are used instead of the system time (see Is used current system time is also used to initialize the generator If x is omitted or None, current system time Optional argument x canīe any hashable object. Initialize the basic random number generator. You can use string.ascii_uppercase or ascii_lowercase to have either lower and uppercase So "size" denotes both length of string and size in bytes. Return(''.join(random.choice(string.ascii_letters) for i in range(size))) Nr = bytearray(random.getrandbits(8) for _ in xrange(size)) After re-reading OP's question, I understand now that it's about raw bytes, not ascii chars string
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |