# 9 Unique Numpy Random Functions to Create Random Data

To create completely random data, we can use the Python NumPy random module. This module has lots of methods that can help us create a different type of data with a different shape or distribution. We may need random data to test our machine learning deep learning model, or when we want our data such that no one can predict, like what’s going to come next on Ludo dice.

Using the random module, we can create one number or lakhs of numbers depending on our needs. So, let’s deep dive into the random module and study each functionality it offers.

Contents

## Syntax of Numpy Random

To use the random module of the numpy library, we need to install numpy on our system.

To install numpy – pip install numpy.

After that, we need to import the module using-

from numpy import random

## Different Functions of Numpy Random module

Following are the 9 ways in which you can generate random data in Python –

1. Rand() function of numpy random
2. Choice(a, size)
3. randint() function of numpy random
4. Uniform()
5. Shuffle()
6. Permutation()
7. randn(*args):
8. seed()
9. random()

## 1. Rand() function of numpy random

Parameters

It takes shape as input. If we want a 1-d array, use just one argument, for 2-d use two parameters.

Random.rand() allows us to create as many floating-point numbers we want, and that is too of any shape as per our needs. All the numbers will be in the range-(0,1). If we do not give any argument, it will generate one random number.

Return Type

It returns float random type values.

1-D array-

```from numpy import random
# if no arguments are passed, we get one number
a=random.rand()
print(a)
```
`0.16901867266512227`

2-D array-

```from numpy import random
# To create an array of shape-(3,4)
a=random.rand(3,4)
print(a)
```
`[[0.61074902 0.8948423 0.05838989 0.05309157] [0.95267435 0.98206308 0.66273378 0.15384441] [0.95962773 0.27196203 0.50494677 0.63709663]]`

## 2. Choice(a, size) To Random Class

It is generally used when we need a random value from specified values. We can give a list of values to choose from or provide a range of values.

Parameters

In the first parameter, we have to specify the values from which the output will be taken. It should only be 1-d

In the second parameter, we have to give the size of the output we want.

Return Type

If we did not give any argument to the size parameter, we would get an integer value. But if we specify any value to the size parameter, we will get an array as output

```from numpy import random
list1=[1,2,5,12,43,99]
#It will select any number of its choice from above list
print((random.choice(list1)))
```
`43`

## 3. randint() function of Numpy Random

It also returns an integer value between a range like randrange(). The difference lies in the parameter ‘b’.

Parameter

‘a’ is the starting parameter which is included, and ‘b’ is the ending range, which is also included. ‘Size’ specifies the number of output we want.

Return Type

It also returns an integer value as well as array.

```from numpy import random
# between 1-100 it can chose any value
print(random.randint(1,100))
```
`12`
```from numpy import random
# between 20-90 it will make an array of shape-(5,4)
print(random.randint(20,90,(5,4)))
```
```array([[36, 57, 37, 46],
[49, 85, 76, 50],
[86, 26, 24, 46],
[40, 62, 58, 66],
[67, 48, 35, 26]])```

## 4. Uniform() Function of Numpy Random

It returns a floating-point value between the given range.

Parameters

It has three parameters. ‘a’ is the starting range, ‘b’ is the ending range, ‘size’ is the size of array we want to create from the given range.

Return Value

It returns a floating-point value.

```from numpy import random
# between 1-10 it will  make an array of shape (2,3) with floating point
# values
print(random.uniform(1,10,(2,3)))
```
```[[1.60076826 4.95001411 9.32575104]
[6.71819493 6.35305451 1.83764578]]```

## 5. Shuffle() Function of Numpy Random

It shuffles the value of the list. We can even give string values in the list.

Parameters

It takes a list as input.

Return Value

It returns None.

```from numpy import random
list1=[1,2,3,4,6,7]
# The change takes place in the list only and it is irreversible
random.shuffle(list1)
print(list1)
```
`[7, 4, 1, 2, 6, 3]`
```from numpy import random
fruits=['apple','mango','cherry','strawberry']
random.shuffle(fruits)
print(fruits)
```
`['strawberry', 'mango', 'cherry', 'apple']`

## 6. Permutation() Function of Numpy Random

Parameters

Takes one input- a.

It returns the number of values in the parameter in any random order. Each value will occur only once.

Return Type

The return type is an array.

```from numpy import random
a=random.permutation(100)
print(a)
```
`[ 2 47 34 62 43 32 44 37 94 18 36 29 91 81 69 76 23 75 48 56 4 72 26 78 24 35 12 97 80 89 5 67 79 86 83 71 59 42 14 68 57 46 84 13 82 77 98 15 53 31 60 99 30 58 33 54 73 19 51 41 74 70 45 27 9 0 25 61 85 66 20 3 65 92 16 96 11 95 7 93 50 1 38 10 52 21 49 63 87 17 55 6 8 39 64 28 40 22 88 90]`

## 7. randn(*args) Function of Numpy Random

It returns the number of values specified in the parameter. The values are floating-point values and in the standard normal distribution. In Standard Normal Distribution, the standard deviation is 1, and the mean is 0.  The range of values will be –3 to 3

Parameters

It can take any number of arguments. If one argument is given, it will be a 1d array. For 3 arguments, it will be a 3d array. If no arguments are given, it will return any random value.

Return Value

Floating point and array

```from numpy import random
a=random.randn()
print(a)
```
`0.5399234948715245`
```from numpy import random
a=random.randn(4,5)
print(a)
```
`[[-0.64325277 -0.54409155 -0.39724942 0.27098597 -0.5790731 ] [-0.40137426 -0.37112032 -1.98715892 -1.43663396 0.38451821] [-0.64054325 -0.00464748 -1.64022962 0.50233829 -0.98809477] [-1.31515342 -0.73362683 0.52486942 -0.21917923 0.01206075]]`

## 8. seed() Function To Generate Output

Parameter

It takes only one argument – seed.

What seed() function does is that it makes the output predictable. The value of output will remain the same every time for the same seed value.

Return Type

It returns None

To better understand it, let us run the below program two times.

```from numpy import random
print(random.rand(5))
```
```[0.79172504 0.52889492 0.56804456 0.92559664 0.07103606]

[0.0871293 0.0202184 0.83261985 0.77815675 0.87001215]```

Now, let us use the seed function and run the program two times.

```from numpy import random
# You can give any seed value, the value for a particular seed value will #remain same every time.
random.seed(0)
print(random.rand(5))
```
```[0.5488135 0.71518937 0.60276338 0.54488318 0.4236548 ]

[0.5488135 0.71518937 0.60276338 0.54488318 0.4236548 ]```

## 9. random() Function

Parameters

It has only one parameter (which is optional), in which we can give the size of the array we want.

Return Value-

Float

```from numpy import random
print(random.random((3,3,3)))
```
```[[[0.88173536 0.69253159 0.72525428]
[0.50132438 0.95608363 0.6439902 ]
[0.42385505 0.60639321 0.0191932 ]]

[[0.30157482 0.66017354 0.29007761]
[0.61801543 0.4287687  0.13547406]
[0.29828233 0.56996491 0.59087276]]

[[0.57432525 0.65320082 0.65210327]
[0.43141844 0.8965466  0.36756187]
[0.43586493 0.89192336 0.80619399]]]```

## Conclusion

There are many functions inside the numpy random module and each of them cannot be discussed here. We have discussed almost every important functions like rand, randint, shuffle, choice and many more of them.

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

Subscribe
Notify of 