Skip to content
Sign upLog in
← Back to Community

How do I make a random number in python?

Profile icon
ThePuzzlerThree

On repl.it, 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 [earned 5 cycles]
View Answer
Voters
Profile icon
TebogoMokgwatsa
Profile icon
JaspreetSingh72
Profile icon
KaseyChung
Profile icon
VladIlchishin
Profile icon
19k7nc
Profile icon
sbiyanirRohan
Profile icon
ArchieCharge
Profile icon
ffsdodo
Profile icon
DavidJamesJame1
Profile icon
RonaldVue
Comments
hotnewtop
Profile icon
DarshanRajpara

this:
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!

Profile icon
UzayAnil

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

@DarshanRajpara

Profile icon
ThePuzzlerThree

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

Profile icon
jameshoward3

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

x = randint(1, 100)
print(x)

Profile icon
1chigo

randint doesn't work.


@jameshoward3

Profile icon
Rowan07k

randint isnt a thing on this site

Profile icon
AdyanKabir

how to generate random numbers

Profile icon
AdamZow

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

a=random.randint(1,100);
print(a);

Profile icon
UzayAnil

here's some code for you to look at: https://repl.it/@UzayAnil/guesspy
in the code, look at lines 1-10.

Profile icon
Vandesm14

Send me the link to your repl.

Profile icon
theangryepicbanana

Try import randint from random

Profile icon
ThePuzzlerThree

@theangryepicbanana
does not work due to incorrect syntax

Profile icon
jameshoward3

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

Profile icon
eco27

you should be able to import math

did you remember to install it as a package?

Profile icon
ThePuzzlerThree

@MrEconomical
which one?:
Screenshot 2019-11-28 at 19.41.24

Profile icon
eco27

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

Profile icon
ThePuzzlerThree

@MrEconomical
i looked up math these were my results
Screenshot 2019-11-28 at 19.55.55

Profile icon
eco27

@ThePuzzlerThree
try pycopy-math I guess

Profile icon
ThePuzzlerThree

@MrEconomical
okay hold on

Profile icon
ThePuzzlerThree

@MrEconomical
the top one?
Screenshot 2019-11-28 at 20.00.18

Profile icon
eco27

@ThePuzzlerThree
yes I think so

Profile icon
ThePuzzlerThree

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

Profile icon
LeonDoesCode

@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)
Profile icon
Jessica0519

@LeonDoesCode
This worked! Thanks!