Ask coding questions

← Back to all posts
How do you generate random numbers in Python?

i just want to know...............................

T11tu5 (0)

These two ways are my favourite:
from random import *
randint (1, 10)
import random
random.randint (1, 10)

christiant (1)

The easiest method to do this is:

import random

random.randint(1, 10)

This simply chooses a random number between 1 and 10. You can change the two numbers to whatever you want just remember that the first number is the minimum and the second number is the maximum.

I hope this helped.

SagaciousPan (22)

The below code will print a random number between 1 and 100 ten times.

# import the 'random' library and it's commands
import random

# do this 10 times
for x in range(10):
  #print a random number between 1 and 100
  print (random.randint(1,101))

Test at:

Hope I could help!

Please mark this as answered if your problem has been solved.

SixBeeps (5052)
TL;DR, you can import the random library, and from there, call random.randrange(start, stop, step) to get a random number