I need to generate numbers for a promotion, these numbers should range from 0 to 99999.
How can I distribute these numbers randomly and equitably, without repeating numbers already distributed?
I need to generate numbers for a promotion, these numbers should range from 0 to 99999.
How can I distribute these numbers randomly and equitably, without repeating numbers already distributed?
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 that these are unique, you can loop and store those 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, from a database to a simple text file. Using a text file, for example, you would have to:
Below is 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
You can use the rand
function.
<?php
echo rand(0, 99999);
?>
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 (1980s).
Below is the logic for anyone interested in testing in any language:
Semente = 0,283746479248234
Report or generate a number between 0 and 1 exclusive . To dynamically change the seed can be done "well-heeled" 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, since this value must be "between" these two values.
Semente = 999 x Semente - INT(999 x Semente)
At this point the new value was generated between 0 and 1 for the seed, that is, redoing the process I am describing, another number will be drawn, although repetitions can occur (repetitions that can be treated according to the previous answers). This INT function should take the whole part "without rounding", otherwise directly take the fractional part of the multiplication result.
Inteiro_mínimo = 0
Inteiro_máximo = 99999
Here the range values of the desired integers are given
Inteiro_máximo = Inteiro_máximo + 1
This is an adaptation I made, because as the equation does not generate the value 1, the result 99999 will never be obtained, as will the integer value of the result, noting that the integer generated by the INT function should not be rounded, adding 1 to this will include the value 99999 in the draw as desired.
Numero_sorteado = INT( semente x Inteiro_máximo + Inteiro mínimo)
The equation "seed x Integer_maximum" will never generate a value of 0, however, it will generate real values greater than zero and less than 999, that is, when it results in values smaller than 1, the INT function (without rounding) will result in a value of 0.
HP reported back then that such a procedure generates a mathematical function that generates little repetition. At the time I chugged to generate several games with this feature, since there was no RANDOM key, and I really noticed that fact. I hope I have contributed.
In Java:
Random random = new Random();
random.nextInt(100000);