Hello guys, what’s up? Welcome to programiz tutorial in python. Today I am going to teach you about 1 to 10 random number generator in python programming language. Just like other object oriented programming (OOP) language, we can generate random number in python programming language. Generating random number in python is a basic program for python programmer. If you don’t know how to generate random number in python, you cannot claim yourself as a python programmer. In machine learning algorithm randomness is an important part of the configuration & evaluation. In machine learning we need to
 Split data into random train and test sets
 Random shuffling of a training dataset in stochastic gradient descent
 Random initialization of weights in an artificial neural network
Anyway guys. Let’s start:—
Random number between 0 and 1.
Function random() is used for generating a random number between 0 and 1 [0, 0.1 .. 1]. Generating random value in between 0 and 1, which will be floating point number:–
from random import * print(random()) # Generate a pseudorandom number between 0 and 1.
If we want to generate random floating point number in between 1 and 10, then we need to use uniform() function.
from random import * print(uniform(1, 10))
1 to 10 random number generator with random.randrange() function
In python programming language random.randrange() function is used for generating random number in a given range. For example, if we want to generate random number in between 20 to 60, then we can use this function.
How to use random.randrange()
Syntax:
random.randrange(start, stop[, step])
Above we can see that random.randrange() function takes 3 parameters. But, here two parameters (start and step) are optional

randrange(): is exclusive function. For example, randrange (10,20,1). It will never select 20. It will only return random number from 10 to 19

Start: starting number in a range. We can say it, lower limit. If we will not specified, by default it assume starts is 0

Stop: last number in a range. We can also say it, upper limit.

Step: difference between each number. It is optional parameters. If we will not specified, by default it assume step value is 0
 We can only use integer value parameters in random.randrange() function. Otherwise it will throw an ValueError.
 if stop <= start or step =0, it will also throw an ValueError.
random.randrange() function Example
By using random.randrange() function below program work as a 1 to 10 random number generator
import random print("Generate random integer number 1 to 10 in Python ") #random.randrange() with only one argument print("Random number between 1 and 10 : ", random.randrange(10))
Output
Generate random integer number within a given range in Python Random number between 1 and 10 : 7
Generate a random number of multiple of n
What if, if you need to print random integer values & those are in between 1 and 100 but values are multiple of 5? You have to use a little more arithmetic so that the random integer is in fact a multiple of five, then everything are same as before we have learned. Let’s check this in our below code:—
import random number = random.randrange(10, 100, 10) print("using randrange First random number multiple of 10 is ", number) number = random.randrange(10, 100, 10) print("Using randrange Second First random number multiple of 10 is ", number)
When above code will execute, our output looks like below−
using randrange First random number multiple of 10 is 70 Using randrange Second First random number multiple of 10 is 30
What if, if you need to print all random value in between 0 to 10. For this, we can use random.shuffle() function. Because it can shuffle/randomize the number.
Note: range() function is mutually exclusive. So, if we set 10 in range() function, it will never select 10.
>>> import random >>> nums = [x for x in range(10)] >>> random.shuffle(nums) >>> nums [6, 3, 5, 4, 0, 1, 2, 9, 8, 7]
Today we have learned 1 to 10 random number generator in python programming language. I hope you guys have understood everything. So guys, that’s all for today. Later I will discuss another tutorial on python programming language. Till then, take care. Happy Coding