LoginSignup
2
6

More than 5 years have passed since last update.

pythonでランダムな文字列を生成する

Last updated at Posted at 2018-07-03

random.choices

import random, string

print(''.join(random.choices(string.ascii_lowercase, k=20)))
# => 'vruffnprgthivdocbpgk'

Return a k sized list of elements chosen from the population with replacement. If the population is empty, raises IndexError.

with replacementなので重複がある。


random.sample

import random, string

print(''.join(random.sample(string.ascii_lowercase, k=20)))
# => 'piwuhekfvrgmaqnxzbcj'

Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

without replacementなので重複はない。

What does replacement mean in numpy.random.choice?

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6