DEV Community

TechPlygrnd
TechPlygrnd

Posted on

NumPy Tutorial #14: Random

Hi everyone, in this blog I will show you how to generate value in NumPy.


Random

To generate value, you can use random from NumPy. It is very easy to use it. Let me show you how to do it.

First, you have to import random

from numpy import random
Enter fullscreen mode Exit fullscreen mode

Then use randint method from random to generate random value. This method requires one argument, which is the maximum constraint that will be generated. In this example, I will set 70 as the maximum constraint, so that the value generated will in between 0 - 70

x = random.randint(70)
Enter fullscreen mode Exit fullscreen mode

If you print x, you will get value around 0 - 70 as the output, in this case the following is my output result (your result might be different)

17
Enter fullscreen mode Exit fullscreen mode

If you want to generate an array of values instead of an individual array, you can add the second optional argument size that requires the tuple of array size you want

arr = random.randint(70, size=(10))
Enter fullscreen mode Exit fullscreen mode

If you print arr, you will get an array that have 10 random values. Below are my output result (your result might be different)

[58 41 55  0 13 21 62  6 38 43]
Enter fullscreen mode Exit fullscreen mode

That is how you can generate value in NumPy. Thank you for reading this blog and have a nice day!

Top comments (0)