Having said that, even if you don't need the fastest solution, reading through this answer might be adventurous and educational. Benchmark section), and they don't increase complexity significantly. Although the first 2 steps ( Bytes and Remainder) might be an acceptable compromise: they do improve performance by like 50% (see exact numbers in the II. The fastest solution is not a go-to solution if you just need a random string. You have to save it into a file named XX_test.go and run it with go test -bench. Repeat step 2 and 3 for the desired length of the string. Append the randomly generated 0 or 1 to the string, key. The code on the Playground is a test file, not an executable. Generate a randomly either 0 or 1 using randint function from random package. Benchmarking each iteration can be found at the end of the answer.Īll the solutions and the benchmarking code can be found on the Go Playground. We'll arrive at our final, fastest code in an iterative manner. String module which contains various string constant which contains the ASCII characters of all cases. To generate a random string we need to use the following two Python modules. The question asks for the "the fastest and simplest way". import random import string random ''.join ( random.choice (string.asciiletters + string.digits ) for n in range (12)) print (random). String = ''.join(RND().Paul's solution provides a simple, general solution. :yield: Yield composed random string if unique :param amount: amount of random strings to generate, defaults to 1 But if you don't want to have Django, here is independent code of it: Code: import random import hashlib import time SECRETKEY 'PUT A RANDOM KEY WITH 50 CHARACTERS LENGTH HERE ' try: random random.SystemRandom () usingsysrandom True except NotImplementedError: import warnings warnings.warn ('A secure pseudo-random number generator is. :param size: string length, defaults to 1 Return x random strings of a fixed length. The generator will stop at a given amount or when the maximum possible combinations are reached.įrom string import ascii_uppercase, digitsĭef string_generator(size: int = 1, amount: int = 1) -> Generator: Note this will cost some memory with very large strings or amounts and it probably will slow down a bit. Return ''.join(random.choice(allowed_chars) for i in range(length))Ī random generator function without duplicates using a set to store values which have been generated before. # is better than absolute predictability. # properties of the chosen random sequence slightly, but this # using a value that is hard for an attacker to predict, every # This is ugly, and a hack, but it makes things better than Falling back to Mersenne Twister.')Īllowed_chars='abcdefghijklmnopqrstuvwxyz' Im creating a python script which should create a new file every time it generates a new string (everytime the script is executed it prints out the string to file), and also compares this random string with a string which is a text like: THIS IS A STRING, which is located on another already created file. here is what i am doing: suffix ''. Warnings.warn('A secure pseudo-random number generator is not available ' i need to create a string consisting of spaces, and the length of the string needs to be determined at runtime. SECRET_KEY = 'PUT A RANDOM KEY WITH 50 CHARACTERS LENGTH HERE !!' Get_random_string(4, allowed_chars='bqDE56')īut if you don't want to have Django, here is independent code of it: The default length of 12 with the a-z, A-Z, 0-9 character set returnsĪ 71-bit value. Returns a securely generated random string. get_random_string(length=12,Īllowed_chars=u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') By Django, you can use get_random_string function in module.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |