Skip to content

Instantly share code, notes, and snippets.

@1cadumagalhaes
Last active December 30, 2022 13:40
Show Gist options
  • Save 1cadumagalhaes/c0f00487c92b8822165f001419579f69 to your computer and use it in GitHub Desktop.
Save 1cadumagalhaes/c0f00487c92b8822165f001419579f69 to your computer and use it in GitHub Desktop.
Algoritmos completamente desnecessários mas divertidos de se pensar.
function generateArray(numbers = 60) {
return Array.from({ length: 60 }, (_, i) => i + 1)
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array
}
function random(min = 1, max = 10) {
return (
Math.floor(
Math.pow(10, 14) * Math.random() * Math.random()) % (max - min + 1)
) + min;
}
function sorteio(total = 60, pools = 6) {
console.info(`Executando o sorteio de ${pools} numberos de ${total} em node.js`);
const pool_size = total / pools;
let numbers = generateArray()
let result = [];
for (let i = 0; i < 6; i++) {
numbers = shuffleArray(numbers);
let current_pool = shuffleArray(numbers.splice(0, pool_size));
let index = random(0, pool_size - 1);
result.push(current_pool.splice(index, 1)[0]);
}
return result;
}
console.log(sorteio());

Plano de um desocupado para ganhar na mega da virada

Estratégia

Ao invés de somente usar geradores de números pseudo-aleatoŕios, aumentei a complexidade tentando emular o comportamento do sorteio real.

Por isso, esses códigos:

  • Geram a lista de números (no caso da mega-sena, 60)
  • Embaralha a lista e quebra em 6 pools de 10 números cada
  • Embaralha cada pool
  • Para cada uma:
    • Escolhe um índice aleatório
    • Remove o número desse índice
from random import shuffle, seed, randint
def sorteio(total=60, pools=6):
print(f"Executando o sorteio de {pools} numeros de {total} em python")
numbers = list(range(1, total + 1))
results = []
pool_size = total // pools
for _i in range(0, pools):
seed()
shuffle(numbers)
current_pool = numbers[:pool_size]
del numbers[:pool_size]
shuffle(current_pool)
index = randint(0, pool_size - 1)
current_number = current_pool.pop(index)
results.append(current_number)
return results
print(sorteio())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment