문자열 랜덤하게 생성하기
조회수 342회
leng=int(input())
def gerRandomString(leng):
import random
num=random.randrange(97,123)
for i in range(1,leng+1):
num
num=chr(num)
word=[]
for i in range(0,leng):
word.append(num)
b="".join(word)
print(b)
gerRandomString(leng)
여기서 실행을 시키면 계속 같은 문자가 입력한 수만큼 나오게 되는데 서로 다른 문자가 입력한 숫자만큼 나오게 하려면 어떻게 해야 하나요? 중복된 문자여도 상관없습니다.
예: 8 입력시 abjunica , 5입력시 ackde
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> chr(97) 'a' >>> chr(123) '{' >>> [ chr(e) for e in range(97,123) ] ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] >>> import random >>> random.sample('abcdefghijklmnopqrstuvwxyz', 3) ['z', 'c', 'q'] >>> l = 5 >>> random.sample('abcdefghijklmnopqrstuvwxyz', l) ['j', 'x', 'w', 'e', 'z'] >>> ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', l)) 'rmevu' >>> l = 10 >>> ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', l)) 'dpilbfryth' >>> ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', l)) 'jsnuwfvibl' >>> l = 33 >>> ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', l)) Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', l)) File "C:\PROGRAMS\Python3864\lib\random.py", line 363, in sample raise ValueError("Sample larger than population or is negative") ValueError: Sample larger than population or is negative >>> help(random.sample) Help on method sample in module random: sample(population, k) method of random.Random instance Chooses k unique random elements from a population sequence or set. Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices). Members of the population need not be hashable or unique. If the population contains repeats, then each occurrence is a possible selection in the sample. To choose a sample in a range of integers, use range as an argument. This is especially fast and space efficient for sampling from a large population: sample(range(10000000), 60) >>> help(random.choices) Help on method choices in module random: choices(population, weights=None, *, cum_weights=None, k=1) method of random.Random instance Return a k sized list of population elements chosen with replacement. If the relative weights or cumulative weights are not specified, the selections are made with equal probability. >>> l = 33 >>> ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', l)) Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', l)) File "C:\PROGRAMS\Python3864\lib\random.py", line 400, in choices cum_weights = list(_accumulate(weights)) TypeError: 'int' object is not iterable >>> ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=l)) 'lrrhjwyjxjhmzzhznalkzfqsczrwaarzr' >>> l = 10 >>> ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=l)) 'cdekxhpcil' >>>
댓글 입력