How to use Python Random.Seed()?

Asked

Viewed 2,470 times

2

I need to generate a random number using the Python language, in the documentation saw that there is a function random.seed() to increase the randomness of the generated number, but I found very confusing the description of this function.

Can anyone better explain how I should use it to increase randomness? Use in conjunction with the module datetime? I must use every time before I perform random.random() or call her once enough?

  • 2
  • The doubt needs to always use Random.Seed() before calling Random.Random() or not? The Python function Random.Random() of the library already generates a Seed internally?

  • 2

    Random already generates a Seed, you can set a manually Seed, this is useful to test some procedures, because you already know the numbers generated can predict the behavior and "debug" correctly.

2 answers

4


To increase randomness, make use of random.SystemRandom():

The following happens: the most visited part of the official documentation shows "chargeable" that look like functions, in the module random - do

import random

a = random.randint(...)
b = random.choice(...)

But these "functions" are actually methods of a class random.Random that are exposed directly in the module for ease of use.

So if you create an instance of random.Random - will have as methods these same calls - randint, randrange, Choice, etc... and a seed. Only that the module random has also the specialized class SystemRandom that uses as source of the random numbers the operating system API for this. That is, in Linux takes values from /dev/random, in Windows uses CryptGenRandom, etc... These random numbers of the system are, as far as possible, guaranteed random enough for use in crypto tasks, etc... and far beyond the pseudo-random ones that the random.Random normal provides.

So just create an instance of SystemRandom and call the methods of that instance instead of the normal functions of the module Random:

from random import SystemRandom

random = SystemRandom ()

a = random.randint(...)
b = random.choice(...)

As to the seed, on objects SystemRandom the function seed exists, by virtue of the object orientation model, in which specialized classes have to expose the methods and attributes of parents, but in fact, it is not used - it is even in the documentation of it: Stub method. Not used for a system random number generator.

The random data source used by SystemRandom is exposed in the module os and can also be used directly with the call os.urandom(n) - and returns a byte sequence with the values provided by the operating system. The SystemRandom encapsulates this call and already makes available all the practical methods we know - randint, Choice, Uniform, etc...

2

Hello, the python it does not generate a random number, they are pseudo-random or be deterministic. It is generated from the number you put in random.seed()! Here’s an example I did.

#-*- coding: utf-8 -*-
#Meu Décimo quinto Programa
#Trabalhando com numerosAleatorios

#importar a biblioteca
import random

#Uma seed(semente) de numero 10.

random.seed(10)
#gera 10 numeros aleatórios
for i in range(10):
#Escolhe um numero aleatório entre 0 e 999
    numero = random.randint(0,1000)
    print(numero)

When running this code multiple times you don’t get different results for this range range (generated 10 numbers) because Seed gets hooked and always generates the same numbers. so it is interesting to put a random value as parameter for the Seed

Browser other questions tagged

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