What is Random Number Generator in Python and how to use it?
Introduction:
- We can be able to generate the Random integer values by using the randint() function.
- When we are going to use the function randint() then it is used to takes two arguments such as start and end.
- Here the start and the end are used to denote the range for the integer to be generated values.
- When we are concern about the Random integers, then they are used to be get generated within the boundary values. Here the boundary values are the start and end of range values.
- More specifically they are the interval [start, end].
Here, I am going to discuss about How to generate the Random Integer in Python. Here we are also going to discuss that how they are used to support the inbuilt functions which is being provided by the Python library.
What is Random Number Generator in Python:
- A Random Number Generator in Python is a function which is used to generate the random number whenever it is get called.
- It is a built-in function in Python.
- It is a function which is present in the Python Random module.
- So, whenever we need to generate the random number we need to call this module and invoke the Generate method.
- Along with this method this module also consists of other random number generator functions methods such as seed(), randrange(), randint(), choice(), shuffle(), sample() and uniform().
- These methods are also called as supporting methods for Random number generation.
- The seed() method is basically used to produce the values which are deterministic in nature.
- A deterministic value are used to have the same sequence of values.
The randrange() method is basically used when we need to return random values between the specified limit and interval. - The randint() is basically used when we are going to returns a random integer as per the given limit.
- The choice() method is basically get used when we need to return a random number form a given sequence of numbers.
- The suffle() method is basically used when we need a suffle a value within a given sequence of numbers.
- The sample() method is basically get used when we need to return a randomly selected values.
- The uniform() method is basically get used when we need to returns the floating-point values form a given range of values
Generating integers:
- As we have already discussed that if we are going to generate the Random integers values then it can be get generated using functions such as randrange() and randint() methods.
- The randrange() method is basically used when we need to return random values between the specified limit and interval.
- Similarly, The randint() is basically used when we are going to returns a random integer as per the given limit.
Let us consider the example as discussed below which will let you know how to use these methods.
randrange():
- As we know that the randrange() function is basically used when we need to return random values between the specified limit and interval.
- It is basically used to allow the user to generate values by stepping over the interval count.
Syntax:
randrange()
Let us consider the following example which shows the use of randrange().
import random
for x in range(5):
print(random.randrange(2,60,2))
randint():
- When we are going to use the randint() method then it is basically used to generates integers between a given limit.
- Here It will takes two parameters as an input which are used to specifies the limit values.
- Here lower limit is get specified by first parameter whereas the second specifies the upper limit.
Syntax:
randint(a,b)
here a is the lower limit and b is the upper limit.
Example:
import random
random.randint(1,5)
Similarly, if we need to generate the series of sequence then we may take the approach of loop here. But it should be get noted that we need to put the function as a body of the loop.
For Example:
import random
for x in range(2):
print(random.randint(2,11))
Generating floating-point numbers:
- Like similar to above if we are going to generate the floating point number then we can use the random() and uniform() method.
- When we are going to use the random() method then it produces floating-point values between 0.0 to 1.0.
- Like other method it never used to takes any parameters.
Here the upper limit is excluded. So, the maximum value it can range up to 9.999. - When we are going to use the uniform() method then it is basically get used when we need to returns the floating-point values form a given range of values
Let us consider the following example which will let you know about the use of random() and uniform().
Example:
Use of random():
import random
for x in range(5):
print(random.random())
Use of uniform():
for x in range(5):
print(random.uniform(6))
Generating values from a given sequence:
- When we are going to generate the values form a sequence then we use the choice().
- The choice() method is basically get used when we need to return a random number form a given sequence of numbers.
Let us consider the following example:
for x in range(3):
print(random.choice([1,2,3,4,5,6,7,8,9]))
Like similar to choice () some times we may also use the sample(). The use of sample() is almost similar as that of choice().
Let us consider the following example which will let you know how to use the sample().
x=random.sample([1,2,3,4,5,6,7,8,9],4)
print(x)
Other functions:
As we have already discussed above earlier that the other functions are also present which are used to generate the random number. Let us discuss one by one here.
shuffle():
- The suffle() method is basically used when we need a suffle a value within a given sequence of numbers
Example:
x=[1,2,3,4,5,6,7,8,9]
random.shuffle(x)
print(x)
seed():
- This method is also called as supporting methods for Random number generation.
- The seed() method is basically used to produce the values which are deterministic in nature.
- A deterministic value is used to have the same sequence of values.
Let us consider the following example as below:
random.seed(2)
print(random.random(),random.random(),random.random(),end=’nn’)
It should be get noted that This function is very useful when we need to pass the same random numbers to various test cases.
Scope @ NareshIT:
- At Naresh IT you will get a good Experienced faculty who will guide you, mentor you and nurture you to achieve your dream goal.
- Here you will get a good hand on practice in terms of practical industry-oriented environment which will definitely help you a lot to shape your future.
- During the designing process of application, we will let you know about the other aspect of the application too.
- Our Expert trainer will let you know about every in’s and out’s about the problem scenario.
Achieving your dream goal is our motto. Our excellent team is working restlessly for our students to click their target. So, believe on us and our advice, and we assured you about your sure success.