Development Tip

임의의 문자열과 임의의 16 진수를 만드는 가장 간단한 방법

yourdevel 2020. 10. 17. 12:27
반응형

임의의 문자열과 임의의 16 진수를 만드는 가장 간단한 방법


다음과 같이 임의의 30 자 문자열을 만드는 가장 간단한 방법은 무엇입니까?

ufhy3skj5nca0d2dfh9hwd2tbk9sw1

다음과 같은 16 진수 30 자리 숫자?

8c6f78ac23b4a7b8c0182d7a89e9b1


16 진수 출력을 위해 더 빠른 것을 얻었습니다. 위와 동일한 t1 및 t2 사용 :

>>> t1 = timeit.Timer("''.join(random.choice('0123456789abcdef') for n in xrange(30))", "import random")
>>> t2 = timeit.Timer("binascii.b2a_hex(os.urandom(15))", "import os, binascii")
>>> t3 = timeit.Timer("'%030x' % random.randrange(16**30)", "import random")
>>> for t in t1, t2, t3:
...     t.timeit()
... 
28.165037870407104
9.0292739868164062
5.2836320400238037

t3 임의 모듈을 한 번만 호출하고 목록을 작성하거나 읽을 필요가 없으며 나머지는 문자열 형식으로 처리합니다.


30 자리 16 진수 문자열 :

>>> import os,binascii
>>> print binascii.b2a_hex(os.urandom(15))
"c84766ca4a3ce52c3602bbf02ad1f7"

장점은 이것이 OS에서 직접 임의성을 가져오고 random ()보다 더 안전하고 /하거나 빠르며, 시드 할 필요가 없다는 것입니다.


Py3.6 +에서 또 다른 옵션은 새로운 표준 secrets모듈 을 사용하는 것입니다 .

>>> import secrets
>>> secrets.token_hex(15)
'8d9bad5b43259c6ee27d9aadc7b832'
>>> secrets.token_urlsafe(22)   # may include '_-' unclear if that is acceptable
'teRq7IqhaRU0S3euX1ji9f58WzUkrg'

import string
import random
lst = [random.choice(string.ascii_letters + string.digits) for n in xrange(30)]
str = "".join(lst)
print str
ocwbKCiuAJLRJgM1bWNV1TPSH0F2Lb

여기보다 훨씬 빠른 솔루션 :

timeit("'%0x' % getrandbits(30 * 4)", "from random import getrandbits")
0.8056681156158447

참고 : random.choice(string.hexdigits)(소문자와 대문자 모두) string.hexdigits반환 되므로 잘못된 0123456789abcdefABCDEF결과를 얻을 수 있으므로 16 진수 'c'가 숫자 '7'보다 2 배 더 많이 나타날 가능성이 있습니다. 대신 random.choice('0123456789abcdef').


다른 방법 :

from Crypto import Random
import binascii

my_hex_value = binascii.hexlify(Random.get_random_bytes(30))

요점은 다음 과 같습니다 . 바이트 값은 항상 16 진수 값과 같습니다 .


단선 기능 :

import random
import string

def generate_random_key(length):
    return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(length))

print generate_random_key(30)

덧붙여서, 이것은 timeit제안 된 두 가지 접근 방식을 사용한 결과입니다 .

사용 random.choice():

>>> t1 = timeit.Timer("''.join(random.choice(string.hexdigits) for n in xrange(30))", "import random, string")
>>> t1.timeit()
69.558588027954102

사용 binascii.b2a_hex():

>>> t2 = timeit.Timer("binascii.b2a_hex(os.urandom(15))", "import os, binascii")
>>> t2.timeit()
16.288421154022217

jcdyer가 언급 한 것에 비해 더 빠른 것이 있습니다. 이것은 그의 가장 빠른 방법의 ~ 50 %를 차지합니다.

from numpy.random.mtrand import RandomState
import binascii
rand = RandomState()

lo = 1000000000000000
hi = 999999999999999999
binascii.b2a_hex(rand.randint(lo, hi, 2).tostring())[:30]

>>> timeit.Timer("binascii.b2a_hex(rand.randint(lo,hi,2).tostring())[:30]", \
...                 'from __main__ import lo,hi,rand,binascii').timeit()
1.648831844329834         <-- this is on python 2.6.6
2.253110885620117         <-- this on python 2.7.5

base64에서 원하는 경우 :

binascii.b2a_base64(rand.randint(lo, hi, 3).tostring())[:30]

randint (마지막 인수)에 전달 된 크기 매개 변수를 변경하여 요구 사항에 따라 출력 길이를 변경할 수 있습니다. 따라서 60 자의 경우 :

binascii.b2a_hex(rand.randint(lo, hi, 4).tostring())[:60]

In [1]: import random                                    

In [2]: hex(random.getrandbits(16))                      
Out[2]: '0x3b19'

adding one more answer to the mix that performs faster than @eemz solution and is also fully alphanumeric. Note that this does not give you a hexidecimal answer.

import random
import string

LETTERS_AND_DIGITS = string.ascii_letters + string.digits

def random_choice_algo(width):
  return ''.join(random.choice(LETTERS_AND_DIGITS) for i in range(width))

def random_choices_algo(width):
  return ''.join(random.choices(LETTERS_AND_DIGITS, k=width))


print(generate_random_string(10))
# prints "48uTwINW1D"

a quick benchmark yields

from timeit import timeit
from functools import partial

arg_width = 10
print("random_choice_algo", timeit(partial(random_choice_algo, arg_width)))
# random_choice_algo 8.180561417000717
print("random_choices_algo", timeit(partial(random_choices_algo, arg_width)))
# random_choices_algo 3.172438014007639

참고URL : https://stackoverflow.com/questions/2782229/most-lightweight-way-to-create-a-random-string-and-a-random-hexadecimal-number

반응형