Dice launch simulator in Python

2

I am trying to create a function that every time it is called, generate a list of 100 random launches of a die using the random library.

import random

def dice():
    dado = []
    for i in range(100):
        dado.append(random.choice(range(1,7)))
        return dado

dice()

When I run the function, it only returns a value to me. On the other hand, when I do this:

def dice():
    dado = []
    for i in range(100):
        dado.append(random.choice(range(1,7)))
        print(dado)

It returns 100 lists to me. The first contains a number, the next two, and so on until the 100 list is printed with 100 numbers. As I pointed out, what I want is for a single list of 100 random numbers to be printed.

I want to point out that I am also interested in the fact that the resulting list can be plotted through a histogram, for example.

Any help or guidance is very much appreciated. Thanks!

    
asked by Alejandro Carrera 04.09.2017 в 08:38
source

2 answers

1

The error is in the function "says ()", the return should have a less tabulated, to be returned when the loop ends.

def dice():
  dado = []
  for i in range(100):
      dado.append(random.choice(range(1,7)))
  return dado

I'll give you an example on this link click

    
answered by 04.09.2017 / 09:03
source
0

The throws of dice, you can take it as a succession of random events, without complicating the problem adding conditional probability, you could use the python library random :

import random
# siendo N el numero de elementos de tu lista(numero de tiradas)
def dado(n):
    result=[]
    for i in xrange(0,n):
        result.append(random.randint(1, 6))
    return result

dado(10)
Out[0]:[2, 5, 1, 3, 4, 4, 5, 1, 1, 4]
    
answered by 04.09.2017 в 08:57