import random
mensaje='''Ingrese el número de dados que desea simular y el simulador hará el lanzamiento de dados'''
print(mensaje)
n=int(input('Ingrese el número de dados que desea simular: '))
m=int(input('Ingrese el número de caras de los dados'))
lanzamientos=[]
for i inrange(n):
tiro=random.randint(1,m)
lanzamientos.append(tiro)
print(f'Los lanzamientos del dado salieron: {lanzamientos}')```
Reto 35: Números aleatorios 2 en JavaScript y en Python:
// Números Aleatorios 2 en JavaScriptfunctionrandomNumbers2(dices, faces) { let results = newArray() for (let d = 0; d < dices; d++) { results.push(Math.ceil(Math.random() * faces)) } return results } const numeroDados = parseInt(prompt('¿Cuántos dados vas a tirar?')) const numeroCaras = parseInt(prompt('¿Cuántos caras tienen?')) alert(`Sacaste: ${randomNumbers2(numeroDados, numeroCaras)}`)
# Números Aleatorios 2 en Pythonfrom random import choice defrandomNumbers2(dices, faces): results = [] for d in range(0, dices): results.append(choice(range(1, faces + 1))) return results numeroDados = int(input('¿Cuántos dados vas a tirar?: ')) numeroCaras = int(input('¿Cuántos caras tienen?: ')) print('Sacaste: {}'.format(randomNumbers2(numeroDados, numeroCaras)))
Respuesta:
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Números Aleatorios 2</title></head><style> * { margin: 0; padding: 0; } .contenedor { width: 100%; height: 100vh; min-width: 750px; display: flex; justify-content: center; align-items: center; flex-flow: column; } .contenedorh1 { margin-bottom: 36px; } .inputs { font-size: 18px; display: flex; flex-direction: column; justify-content: center; align-items: center; } .section { margin-bottom: 36px; } .btn { margin: 8px; border-radius: 15px; outline: none; font-size: 24px; border: solid 1px black; color: #000504; text-align: center; padding: 16px; margin-bottom: 36px; } .btn:hover { cursor: pointer; background-color: rgba(0, 0, 0, 0.397); color: white; transform: scale(1.1); } </style><bodyid='body'><divclass="contenedor"><h1>Números Aleatorios</h1><divclass="inputs"><sectionclass="section"> Ingrese la cantidad de dados <inputid="dadosN"/></section><sectionclass="section"> Ingrese la cantidad de caras en los dados <inputid="caraDadosN"/></section></div><buttononclick="lanzar()"class="btn">Repite...</button><pid='resultado'></p></div><script>// const img1 = new Image(); img1.src = './utils/lado1.png'// const img2 = new Image(); img2.src = './utils/lado2.png'// const img3 = new Image(); img3.src = './utils/lado3.png'// const img4 = new Image(); img4.src = './utils/lado4.png'// const img5 = new Image(); img5.src = './utils/lado5.png'// const img6 = new Image(); img6.src = './utils/lado6.png'functionlanzar(){ const dadosN = document.getElementById('dadosN').value const caraDadosN = document.getElementById('caraDadosN').value let dados = [] for (let i = 0; i < dadosN; i++){ dados.push(Math.floor((Math.random()*caraDadosN) + 1)) } (dadosN === '1') ? document.getElementById('resultado').innerHTML = `Tu dado sacó: ${dados}` : document.getElementById('resultado').innerHTML = `Tus ${dadosN} dados sacaron: ${dados}` } /* function cara(caraDado, jugador) { switch(caraDado){ case 1: const image1 = img1.cloneNode() jugador.appendChild(image1) break; case 2: const image2 = img2.cloneNode() jugador.appendChild(image2) break; case 3: const image3 = img3.cloneNode() jugador.appendChild(image3) break; case 4: const image4 = img4.cloneNode() jugador.appendChild(image4) break; case 5: const image5 = img5.cloneNode() jugador.appendChild(image5) break; case 6: const image6 = img6.cloneNode() jugador.appendChild(image6) break; } }*/</script></body></html>
Código en JS y C.
https://github.com/ceporro/PlatziCodingChallenge/tree/master/Day 35-36
😄 Python
import random defgenerate_dices(number_of_dices, number_of_faces): dices = [] faces = [face + 1for face in range(number_of_faces)] # print(faces)for _ in range(number_of_dices): dices.append(faces) return dices defthrow_dices(dices): throws = [] for dice in dices: throw = random.choice(dice) # print(throw) throws.append(throw) score = sum(throws) return (throws, score) defrun(): dices_amount = int(input('How many dices will you throw? ')) dice_faces = int(input('How many faces will your dices have? ')) dices = generate_dices(dices_amount, dice_faces) results, final_score = throw_dices(dices) print(f'\nDices thrown: {results}.') print(f'Final score: {final_score}.') if __name__ == "__main__": run()
En Python 😃
import random mensaje='''Ingrese el número de dados que desea simular y el simulador hará el lanzamiento de dados''' print(mensaje) n=int(input('Ingrese el número de dados que desea simular: ')) m=int(input('Ingrese el número de caras de los dados')) lanzamientos=[] for i inrange(n): tiro=random.randint(1,m) lanzamientos.append(tiro) print(f'Los lanzamientos del dado salieron: {lanzamientos}')```
En JS:
functionrollDices(faceNum, diceNum) { let dices = newArray(); for (let i = 0; i < diceNum; i++) { let randomNumber = Math.floor((Math.random() * (faceNum - 1)) + 1); dices. push(randomNumber); } console.log(dices); }