Movatterモバイル変換


[0]ホーム

URL:


Menu
×
Sign In
+1 Get Certified For Teachers Spaces Plus Get Certified For Teachers Spaces Plus
   ❮     
     ❯   

Random Numbers in NumPy


What is a Random Number?

Random number does NOT mean a different number every time. Random means something that cannot be predicted logically.

Pseudo Random and True Random.

Computers work on programs, and programs are definitive set of instructions. So it means there must be somealgorithm to generate a random number as well.

If there is a program to generate random number it can bepredicted, thus it is not truly random.

Random numbers generated through a generation algorithm are calledpseudo random.

Can we make truly random numbers?

Yes. In order to generate a truly random number on our computers we need to get the random data from someoutside source. This outside source is generally our keystrokes, mouse movements, data on networketc.

We do not need truly random numbers, unless it is related to security (e.g. encryption keys) or the basis ofapplication is the randomness (e.g. Digital roulette wheels).

In this tutorial we will be using pseudo random numbers.


Generate Random Number

NumPy offers therandom module to work with random numbers.

Example

Generate a random integer from 0 to 100:

from numpy import random

x = random.randint(100)

print(x)
Try it Yourself »

Generate Random Float

The random module'srand() method returns a random float between 0 and 1.

Example

Generate a random float from 0 to 1:

from numpy import random

x = random.rand()

print(x)
Try it Yourself »


Generate Random Array

In NumPy we work with arrays, and you can use the two methods from the above examples to make random arrays.

Integers

Therandint() method takes asizeparameter where you can specify the shape of an array.

Example

Generate a 1-D array containing 5 random integers from 0 to 100:

from numpy import random

x=random.randint(100, size=(5))

print(x)
Try it Yourself »

Example

Generate a 2-D array with 3 rows, each row containing 5 random integers from 0 to 100:

from numpy import random

x = random.randint(100, size=(3, 5))

print(x)
Try it Yourself »

Floats

Therand() method also allows you to specify the shape of the array.

Example

Generate a 1-D array containing 5 random floats:

from numpy import random

x = random.rand(5)

print(x)
Try it Yourself »

Example

Generate a 2-D array with 3 rows, each row containing 5 random numbers:

from numpy import random

x = random.rand(3, 5)

print(x)
Try it Yourself »

Generate Random Number From Array

Thechoice() method allows you to generate a random value based on an array of values.

Thechoice() method takes an array as a parameter and randomly returns one of the values.

Example

Return one of the values in an array:

from numpy import random

x = random.choice([3, 5, 7, 9])

print(x)
Try it Yourself »

Thechoice() method also allows you to return anarray of values.

Add asize parameter to specify the shape of the array.

Example

Generate a 2-D array that consists of the values in the array parameter (3, 5, 7, and 9):

from numpy import random

x = random.choice([3, 5, 7, 9], size=(3, 5))

print(x)
Try it Yourself »


 
Track your progress - it's free!
 

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp