## 1 to 100 random number generator in python programming language

Hello guys, what’s up? Welcome to my Programiz tutorial in python. Today I am going to teach you about 1 to 100 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 pseudo-random number between 0 and 1.

```

Generate a random number in between 1 and 100.
Below code will generate a random number in between 1 and 100.

```import random
for x in range(10):
print random.randint(1,101)

```

x in range(10), determines how many random values we want to print. If we want to print 20 numbers, we can set just simply 20 in 2nd line. random.randint(1,101) will select random integer values in between 1 and 100. So, we can say that, above code will print 10 random values of numbers between 1 and 100.

# 1 to 100 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 100 random number generator

```import random

print("Generate random integer number 1 to 100 in Python ")
#random.randrange() with only one argument
print("Random number between 1 and 100 : ", random.randrange(101))

```

Output

```Generate random integer number within a given range in Python
Random number between 1 and 100 :  63

```

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
```
Generate 10 random numbers in between 1 to 100.

```import random

print(random.sample(range(1, 101), 10))

```

After executing the above code our output will be shown as a list:

```[11, 72, 64, 65, 16, 94, 29, 79, 76, 27]

```

Today we have learned 1 to 100 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