# Python program to design a dice throw function

An example of random.choice() in Python: Here, we are going to learn how to design a function that can be used as dice throw and the function will return a random value between 1 to 6?
Submitted by Anuj Singh, on July 31, 2019

Here, we are going to build a dice() function using python. The program is so simple as an introductory program for defining a function. The function is going to use an inbuilt library naming random. This random library helps us to choose a random value of the variable within the range.

```    random.choice([1,2,3,4,5,6])
```

The above function will choose a random value with a probability of 0.167 each i.e. all are independent to each other which will work as dice.

Code:

```import random

# function definition "dice"
# it will return a random value from 1 to 6
def dice():
return random.choice([1,2,3,4,5,6])

# main code
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
print('DICE THREW : ', dice())
```

Output

```DICE THREW :  5
DICE THREW :  3
DICE THREW :  5
DICE THREW :  4
DICE THREW :  3
DICE THREW :  5
DICE THREW :  3
DICE THREW :  3
DICE THREW :  4
DICE THREW :  6
```

TOP Interview Coding Problems/Challenges

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates