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?
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?
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:
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 php java c# python random
You are not signed in. Login or sign up in order to post.
you only need one number?
– rray
you need to choose a language, or you want it to generate in all languages of tags?
– Ricardo Pontual
@Ricardopunctual I do not need a specific language, I would just like to see an approach.
– Matheus SE