All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Random Number Between 0 And 100

Last Updated : Mar 11, 2024

Python Random Number Between 0 And 100

In this tutorial we will show you the solution of python random number between 0 and 100, there is a need to generate random numbers when studying a model or the behavior of a program for a different range of values.

The use of randomness is an important part of the configuration and

evaluation of machine learning algorithms. So, let’s get started.

Step By Step Guide On Python Random Number Between 0 And 100 :-

You can use Python’s built-in functions to achieve the desired result. Python the provides following function to perform this task:

  • random.randint()
  • random.randrange()
import random
number1 = random.randint(0, 100)
print(number1)
number2 = random.randrange(0, 100, 2)
print(number2)
number_list = random.sample(range(0, 100), 10)
print(number_list)
  1. The first thing to do is to import the “random” module into the program that will be useful for performing this task.
  2. In the next line, there is a variable named “numbr1” that will store the value returned from the “random.randint()” function.
  3. This function returns a random integer between a given start and stop integer. It takes two parameters. Both are mandatory.
  4. The first parameter is the starting position of the range. The default value is always 0. The second parameter is the end position of the range.
  5. The “random.randint()” function will return an integer number within the range. You can not use float values in this function. It will raise a “ValueError”.
  6. The print statement will display the value of the variable “number1” in the output window.
  7. In the next line, there is a variable named “number2” which will store the value returned from the “random.randrange()” function.
  8. This will also generate random integer numbers within the range specified in the arguments.
  9. It will accept three parameters: the first is the starting position of the range; the second one is the end position of the range; and the last one is the step value, which represents the increment value in the range. In this case, it is 2.
  10. The “random.randrange()” function will not consider the end position of the range while generating random numbers, which means it will never return the number as a result that is specified as the end position of the range.
  11. The “random.randrange()” function can’t use any float values, as it’s similar to the “random.randint()” function. It will raise a “ValueError” as well.
  12. In the next line, there is a print statement that will display the value of the variable “number2”.
  13. In the next line, there is another variable named “number_list” that will store the value returned from the “random.sample()” function.
  14. The “random.sample()” function will return a list that will contain selected random numbers within the range.
  15. It will return a list of unrepeated random numbers within the range. You can use the step parameter, which is similar to the “random.randrange()” function.
  16. In the next line there is a print statement that will display the value of the variable “number_list”.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now generate a random number between 0 and 100 in Python.

The simplest method you can use is the “random.randint()” function. But you can use any method you find helpful.

I hope this tutorial on python random number between 0 and 100 helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪