58 of 151 menu

The randrange method of random module

The randrange method of the random module returns a random number from a range. In the first optional parameter of the method, we specify the beginning of the sequence. In the second mandatory parameter, we set the end. And in the third optional parameter, we pass the step with which the number will be selected.

Syntax

import random random.randrange([start starting beginning outbreak origin onset rise commencement first opening source birth inception entry dawn outset prime spring origination seed cradle breaking threshold morning incipience setout leader proem derivation mother go-off lead-off throw-off kick-off], end, [step move stride pace increment walk foot lead footstep remove thread augment bat lick footpace])

Example

Let's get a number in the range from 0 to 10, with a sampling step of 2:

print(random.randrange(1, 10, 2))

Result of code execution:

5

Example

Now for the randrange method we will specify only the beginning and end of the range:

print(random.randrange(1, 10))

Result of code execution:

4

Example

Now let's specify only one mandatory parameter in the method - the end of the range:

print(random.randrange(10))

Result of code execution:

5

See also

  • method uniform of module random,
    which generates a pseudo-random real number from the range
  • method randint of module random,
    which generates a pseudo-random integer from a range
  • method sample of module random,
    which returns a random sample of elements from a sequence
  • method random of module random,
    which returns a pseudo-random number
  • method seed of module random,
    which initializes a random number
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline