Ask coding questions

← Back to all posts
How do I make a random number in python?
ThePuzzlerThree (7)

On, import Math does not work, so I can not use math.random to make a random number. What do I do to generate a number?

Answered by UzayAnil (24) [earned 5 cycles]
View Answer
DarshanRajpara (16)

import random
x = random.randrange(1,100)

First you import random. then use the function randrange to create the random number, with the first parameter being the lowest number it can generate, and the second being the highest.
Hope this helped you out!

UzayAnil (24)

apparently, your supposed to do:
import random
x = random.randint(1, 100)
print(x) @DarshanRajpara

ThePuzzlerThree (7)

@UzayAnil thank you it worked! I hope you will check out this project when it is finished!

jameshoward3 (2)

@ThePuzzlerThree if you dont want to import whole random lib, you could use
from random import randint

x = randint(1, 100)

AdamZow (9)

If you want just a random integer and not a floating point, here is the solution:


UzayAnil (24)

here's some code for you to look at:
in the code, look at lines 1-10.

Vandesm14 (2727)

Send me the link to your repl.

theangryepicbanana (1696)

Try import randint from random

ThePuzzlerThree (7)

@theangryepicbanana does not work due to incorrect syntax

jameshoward3 (2)

@ThePuzzlerThree its the wrong way around its needs to be
from random import randint

MrEconomical (2296)

you should be able to import math

did you remember to install it as a package?

MrEconomical (2296)

@ThePuzzlerThree just look up "math" and it should be there

ThePuzzlerThree (7)

@MrEconomical i looked up math these were my results

ThePuzzlerThree (7)

@MrEconomical When i try to install pycopy-math, it gives me an error message that the package installation failed.

LeonDoesCode (438)

@ThePuzzlerThree math is part of the standard libaray, so all you have to do is:

import math

But for random integers, you can use:

from random import randint
num = randint(0, 100)