In this article, we show how to create an array of random integers in Python with Numpy.
To create an array of random integers in Python with numpy, we use the random.randint[] function.
Into this random.randint[] function, we specify the range of numbers that we want that the random integers can be selected from and how many integers we want.
In the code below, we select 5 random integers from the range of 1 to 100.
So, first, we must import numpy as np.
We then create a variable named randnums and set it equal to, np.random.randint[1,101,5]
This produces an array of 5 numbers in which we can select from integers 1 to 100.
1 is inclusive and 101 is exclusive, so the possible integers that we can select from is 1 to 100.
We then display the contents of randnums, which is a random array of 5 integers.
We'll now show one more example.
We're going to create an array of 10 integers that can select from integers to 1-25.
This is shown in the code below.
So now you see an array of 10 random integers.
However, random arrays are not confined to single-dimensional arrays. Arrays can also be multidimensional.
To create random multidimensional arrays, we specify a size attribute and that tells us the size of the array.
For example, if we want an array of 4x5 [4 rows and 5 columns], we specify size= [4,5].
Below is the code to create a random 4 x 5 array in Python.
So this is how you can generate random multidimensional arrays in Python.
And this is all that is required to create an array of random integers in Python with numpy.
Related Resources
I need to generate a big array [or list] with random numbers [ 10⁵ numbers] . I was trying like that:
vet = random.sample[range[10],100000]
But when I try to run :
vet = random.sample[range[10],10000]
File "/usr/lib/python2.7/random.py", line 320, in sample raise ValueError["sample larger than population"] ValueError: sample larger than population
Any solution?
tkns
asked Aug 28, 2012 at 21:36
1
What you want is
[random.random[] for _ in xrange[100000]]
From the random module documentation:
random.sample[population, k] Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.
so when calling random.sample[range[10], 100000]
you're trying to extract 100000 unique elements in a sequence of length 10 which obviously can't work.
Note that
random.random[]
returns a floating value between [0 ; 1]random.randrange[[start], stop[, step]]
returns a random element from the sequencerange[[start], stop[, step]]
random.randint[a, b]
returns an integer value in [a ; b]- when using
random.sample
, the equalitylen[population] >= k
must hold
ThisClark
13.6k10 gold badges64 silver badges96 bronze badges
answered Aug 28, 2012 at 21:41
marchelblingmarchelbling
1,95915 silver badges23 bronze badges
2
I think you're after something like this:
vet = [random.randint[1,10] for _ in range[100000]]
answered Aug 28, 2012 at 21:39
g.d.d.cg.d.d.c
45.1k8 gold badges98 silver badges109 bronze badges
You can you the numpy function and create an array with N space filled with a random number
import numpy as np
vector_size = 10000
one_dimensional_array = np.random.rand[vector_size]
two_dimensional_array = np.random.rand[vector_size, 2]
tree_dimensional_array = np.random.rand[vector_size, 3]
#and so on
numpy.random.rand
You can create matrix of random numbers using the function below and arrays also.
answered Apr 24, 2018 at 15:49