How to generate random numbers for Draw?

Asked

Viewed 2,266 times

4

I need to generate numbers for a promotion, these numbers should go from 0 to 99999.

How can I distribute these numbers randomly and fairly, without repeating numbers already distributed?

  • you only need one number?

  • 1

    you need to choose a language, or you want it to generate in all languages of tags?

  • @Ricardopunctual I do not need a specific language, I would just like to see an approach.

4 answers

9


In Python you can use randint. Ex:

from random import randint
numero = randint(0, 99999)

If you need to generate more than one number, and these are unique, you can loop and store these numbers in a list.

qtd_pessoas = 10
lista_numeros = []
for pessoa in range(qtd_pessoas):
    if numero not in lista_numeros:
        lista_numeros.append(numero)

If you have to generate these numbers in different runs instead of generating them all in the same run, you will need to use some storage medium, ranging from a database to a simple text file. Using a text file, for example, you would have to:

  1. Read the file.
  2. Generate a list of the numbers in this file.
  3. Generate a random number and check if it is not in the generated list in step 2.
  4. If it is not in this list, it is added to the file and the program ends. If it is in the list, you repeat step 2.

Below an example using randint, file manipulation, splitlines, while and break.

#!/usr/bin/env python3
import os
from random import randint

ARQUIVO = 'numeros.txt'

# Caso o arquivo seja criado manualmente, essa parte é descenessária.
if ARQUIVO not in os.listdir():
    # O arquivo não existe, então é necessário cria-lo.
    with open(ARQUIVO, 'w') as arq:
        arq.write('')

# O bloco abaixo vai ler o arquivo e vai adicionar os números salvos
# na lista_numeros
with open(ARQUIVO, 'r') as arq:
    lista_numeros = arq.read()
    lista_numeros = lista_numeros.splitlines()

# Criamos um loop infinito, que vai ser executado que um números que não
# esteja na lista seja criado, e seja executada a instrução "break"
while True:
    numero = str(randint(0, 9999)) # Vamos trabalhar com string na hora de gravar.
    if numero not in lista_numeros:
        with open(ARQUIVO, 'a') as arq:
            arq.write(numero + '\n')
            print(numero)
            break
  • Thank you very much!

7

You can use the function rand.

<?php
echo rand(0, 99999);

?>

6

For a "logical" solution as indicated in the Tags, I adapted a formula to generate random numbers that I had in the manual of the old scientific calculator HP 35E (eighties).

Below I expose the logic to anyone interested in testing in any language:

Semente = 0,283746479248234 

Inform or generate a number between 0 and 1 exclusive. To dynamically change the seed can be made "well-baked" using, for example, the numerical value of Hours, Minutes and Seconds so that it generates a value between 0 and 1. This value can be checked if it resulted in 0 or 1 to be generated again, since this value must be "between" these two values.

 Semente = 999 x Semente - INT(999 x Semente)

At this moment the new value between 0 and 1 was generated for the seed, that is, remaking the process I am describing, another number will be drawn, although repetitions may occur (repetitions that can be treated according to the previous answers). This INT function should take the whole "no rounding" part, otherwise take directly the fractional part of the multiplication result.

  Inteiro_mínimo = 0

  Inteiro_máximo = 99999

Here are entered the values of the desired integer range

  Inteiro_máximo = Inteiro_máximo + 1

This is an adaptation that I made, because the equation does not generate the value 1, will never be obtained the result 99999, as will be taken the integer value of the result, noting that the integer generated by the function INT should not be rounded, by adding 1 this will include the value 99999 in the draw as desired.

  Numero_sorteado = INT( semente x Inteiro_máximo + Inteiro mínimo)

The equation "Integer x Integer" will never generate the value 0, however, it will generate Real values greater than zero and less than 999, that is, when it results in values less than 1, the function INT (without rounding) will result in the value 0.

HP reported at the time that such a procedure generates a mathematical function that generates little repetition. At the time I got to generate several games with this feature, since there was no RANDOM key, and I really noticed this fact. I hope to have contributed.

5

In Java:

Random random = new Random();
random.nextInt(100000);

Browser other questions tagged

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