DEVELOP A SIMULATOR FOR GENERATING RANDOM NUMBER BETWEEN
DEVELOP A SIMULATOR FOR GENERATING RANDOM NUMBER BETWEEN 1 AND 6 By: M. Govinda Rao PGT, Comp. Sc.
Prerequisite: �What is random number in common language ? �What is function? �What is inbuilt function ? �How we include header file in the python program ?
ANS ? Ø Made, done, or happening, without method or conscious decision. Ø It is set of instruction under one umbrella/ name to perform a particular task. Ø Functions used in the python by importing the specific module for various inbuilt function. Ø With the help of import keyword # import [moudule name]
How to generator Random no 1. Import the random module in the program by the following syntax # import random 2. This module implements pseudo-random number generators for various distributions. 3. Almost all module functions depend on the basic function random(), which generates a random float uniformly in the semi-open range [0. 0, 1. 0). The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.
� The random function is used to generate pseudorandom floating point values. � It takes no parameters and returns values uniformly distributed between 0 and 1 (including 0 but excluding 1). >>> from random import random >>> random() 0. 79432800912898816 >>> random() 0. 00049858619405451776 >>> random() 0. 1341231400816878 >>> random() 0. 98724554535361653 >>> random() 0. 21429424175032197 >>> random() 0. 23903583712127141 >>> random() 0. 72918328843408919
Rand. Range() Function �The randrange function is used to select a pseudorandom int from a given range. �Example: randrange(1, 7) returns some number from [1, 2, 3, 4, 6] and randrange(5, 105, 5) returns a multiple of 5 between 5 and 100, inclusive.
Each call to randrange() generates a new pseudorandom int. >>> 5 >>> 3 >>> 2 >>> 5 >>> from random import randrange(1, 6) randrange(1, 6)
The randint() Function Each call to randint() generates a new pseudorandom int. >>> random. randint(3, 8) Output is: 4
�The value 5 comes up over half the time, demonstrating the probabilistic nature of random numbers. �Over time, may or may not be this function will produce a uniform distribution, which means that all values will appear an approximately equal number of times.
USES OF SIMULATION �Simulation can solve real-world problems by modeling real-world processes to provide otherwise unobtainable information. �Computer simulation is used to predict the weather, design aircraft, create special effects for movies, etc.
�Simulations require events to occur with a certain likelihood. These sorts of simulations are called Monte Carlo simulations because the results depend on “chance” probabilities. �A pseudorandom number generator works by starting with a seed value. This value is given to a function to produce a “random” number. �This sequence of numbers appears to be random, but if you start the process over again with the same seed number, you’ll get the same sequence of “random” numbers.
CODING FOR SIMULATION OF DISE GENERATING 1 TO 6 NUMBER � import random � question = input('Would you like to roll the dice [y/n]? n') � while question != "n": � if question == "y": � die 1 = random. randint(1, 6) � #die 2 = random. randint(1, 6) � print(die 1) � question = input('Would you like to roll the dice [y/n]? n') � else: � print('Invalid response. Please type "y" or "n". ') � question = input('Would you like to roll the dice [y/n]? n') � print('Good-bye!')
HOME ASSINGMENT �What is random no in python ? �Define the randint()? �Which module should be imported to use randomint function. �Write a statement to generate random no(100 to 199). .
THANKS
- Slides: 14