1

Is there any way to take multiple values with "random.randrange"? Like in random.choices or random.sample we can choose more than 1 element with "k".

import random
print("3 values divided by 3")
print(random.randrange(100,999,5,k=3,end=','))
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Kobra
  • 19
  • 3
  • Related: [Generate 'n' unique random numbers within a range](https://stackoverflow.com/q/22842289/4518341), [How do I create a list of random numbers without duplicates?](https://stackoverflow.com/q/9755538/4518341) – wjandrea Sep 05 '20 at 19:17

1 Answers1

1

There aren't specific functions to do this in the random module, but look at the docs for randrange():

This is equivalent to choice(range(start, stop, step)), but doesn't actually build a range object.

Therefore, just do choices(range(...), k=3) or sample(range(...), k=3).

Note: In Python 3, this will be memory-efficient since range is lazy. In Python 2, I believe xrange behaves the same.

wjandrea
  • 28,235
  • 9
  • 60
  • 81