Write Python program as a random number generator that generates random numbers between 1 and 6 (simulates a dice)|Using random module|Make a dice simulator using python
Introduction
In this blog you will learn to make a random number generator in python.
For this purpose we will use Random module.
Random Module
The Random module is a module to generate the random variables. It can be used to get a random number, selecting a random elements from a list, shuffle elements randomly, etc.
Random functions
- random.random():-it is a function which dont need any parameters.It is used to float number between 0.0 to 1.0.
- random.randrange(start,stop,step):-This function returns a randomly selected element from the range created by the start, stop and step arguments.
- random.randint(start,end):-This function returns a random integer between the start and the end as specified by the user.
- random.choice():-This function returns a randomly selected element from a sequence(list,tuple..etc).
- random.shuffle():-this function is used to randomly reorder the elements in a list.
Execution
You can also try these random module codes on your IDLE...
Source code-
import randomwhile True:
print("Rolling a dice,")
print("Number is :",random.randint(1,6))
ch=input("want to roll dice once more? y/n:")
if ch=='n':
break
#-------------code ended-----------
- Library used - random
Functions and operators used in the program are:
- while loop
- print()
- break
- random.randint()
"Hope it helped"
Have anything in mind? Let me
know in the comment section👇
Have anything in mind? Let me
know in the comment section👇