-6

I am trying to create a function that generates a random number between 0 and 100 using random, I wrote this code but I only get numbers betweem 0 and 1 what can I do to fix this

import random
def randint (min= 0, max = 100):
    num = random.random()
    return num
randint()
  • 4
    [`random.randint(0, 100)`](https://docs.python.org/3/library/random.html#random.randint) or [`random.randrange(0, 101)`](https://docs.python.org/3/library/random.html#random.randrange) – Olvin Roght May 25 '21 at 19:39
  • 2
    Multiply it by 100. – Scott Hunter May 25 '21 at 19:39
  • And then use `floor()` to convert it to an integer. – Barmar May 25 '21 at 19:40
  • Are you trying to reimplement `random.randint()`? If not, just use it. – Barmar May 25 '21 at 19:41
  • Welcome to stack overflow. This is worth looking at [the documentation for the random module](https://docs.python.org/3/library/random.html). In there, you can see that `random.random()` is designed to "Return the next random floating point number in the range \[0.0, 1.0\)", and there's a whole [subsection](https://docs.python.org/3/library/random.html#functions-for-integers) of functions for integers – G. Anderson May 25 '21 at 19:42

3 Answers3

4

try passing the min & value to the randint()

import random
def randint(min=0,max=100):
    a = random.randint(min,max)
    return a

randint()
Ade_1
  • 1,480
  • 1
  • 6
  • 17
1

This may help you using random inside function:

import random 
def RANDOM(i):
    for i in range(100):
        n = []
        n.append(random.randrange(0, 100, 1)) 
        return n

for i in range(100):
    Text = RANDOM(i)
    print(Text)
graj499
  • 87
  • 2
  • 12
0

You probably want to use randrange() or randint(), for this, taken from this guide: https://docs.python.org/3/library/random.html

import random as rn

def randint():
    num = rn.randint(0,100)
    return num

try that. Hope it works