How to generate random numbers in Python?

Asked

Viewed 106,673 times

15

I would like to know how to generate random numbers in Python. I’m with version 3.4.

  • It is worth noting that it is not possible - at least in the present - to generate random numbers. What we generate, when we speak wrongly random numbers, are numbers pseudo random. See more here https://goo.gl/Vr6tPT and here https://goo.gl/2k1Fvg

  • 1

    I already knew that they are pseudo random, when I was studying C. But in day-to-day, they are called even random.

  • It would be if to open the /must/urandom

5 answers

32


from random import randint
print(randint(0,9))

This generates integer numbers between 0 and 9.

It is possible to use several other functions available in documentation. Each one can be better for what you want.

from random import randrange, uniform
print(randrange(0, 9)) #faixa de inteiro
print(uniform(0, 9)) #faixa de ponto flutuante

You can import everything and use what you want:

from random import *
random.seed() #inicia a semente dos número pseudo randômicos
random.randrange(0, 9, 2) # pares entre 0 e 9
random.choice('abcdefghij') # seleciona um dos elementos aleatoriamente
items = [1, 2, 3, 4, 5, 6, 7]
random.shuffle(items) # embaralha os itens aleatoriamente

I put in the Github for future reference.

  • When I have more reputation, I will offer some reward points. It was quite useful.

13

from random import *
print random()
print uniform(10,20)
print randint(100,1000)
print randrange(100,1000,2)

random() returns a float x such that 0 <= x < 1.

uniform(10,20) returns a float x such that 10 <= x < 20.

randint(100,1000) returns a inteiro x such that 100 <= x < 1000.

randrange(100,1000,2) returns a inteiro x such that 100 <= x < 1000 and x is even

  • What each of these codes does?

  • Updated @Mateus Souza

  • 2

    you know what to use from xxx import * It’s not considered good practice, is it? The roblema is that neither who looks at the code, nor automatic tools (Ides) has to know where a given function came from that is found in the middle of the code.

3

I can’t comment yet because I don’t have a score for it, but randint includes the last paragraph, as opposed to randrange, which works more like the rest of Python (closed interval at the beginning and open at the end). So, in the lai0n response, randint(100,1000) includes the 1000 in the possibilities. At least in Python 3.4, which is what I use. Logo, randrange(a, b+1) is the same as randint(a, b)

2

Use the Random method combined with some class of it. ex:

import random

num = int(input('O computador pensou em um numero entre 0 e 5, você é capaz de advinhar? '))
sorteio = random.randint(0, 5)

if num == sorteio:
    print('Parabens, o numero pensando pelo computador foi {}, o mesmo do seu!!!'.format(sorteio))
else:
    print('Que pena, você nao acertou!!')

2

I leave here a suggestion function, which generates random numbers (from 0.0 to 1.0) in a vector with a given size:

def gerar():
   from random import random
   tamanho = int(input())
   resposta = [0.0] * tamanho
   for i in range(tamanho):
       resposta[i] = random()
   return resposta

Browser other questions tagged

You are not signed in. Login or sign up in order to post.