How to create a numpy array with random numbers from a given interval?

  • with or without duplicates? - vp_arth
  • possible with duplicates - Alexey Voronov

2 answers 2

Good day. It is possible so:

 import numpy as np array = np.random.randint(50, 60, size=(2, 10)) print(array) 

If you want to make an array of float values, then you need to use the following construction:

 array = np.random.uniform(4, 5, [2, 10]) print(array) 

If you do not plan to use the dimension more than one, then you can write the following structure:

 array = np.random.randint(50, 60, 10) # для int 

Or

 array = np.random.uniform(4, 5, 10) # для float 
  • 3
    I tested it, with numpy many times faster, I will delve into :) - Igor Lavrynenko

For example, a list of random numbers can be created without numpy

 >>> a = [random.choice([i for i in range(100)]) for j in range(111)] >>> a [9, 49, 34, 36, 74, 53, 41, 70, 46, 76, 12, 76, 81, 34, 95, 24, 43, 43, 84, 45, 32, 23, 56, 11, 12, 34, 97, 39, 21, 66, 68, 0, 79, 28, 62, 68, 86, 16, 91, 20, 78, 56, 96, 97, 10, 60, 57, 20, 44, 2, 29, 8, 17, 27, 11, 76, 24, 59, 71, 40, 18, 30, 25, 40, 55, 17, 94, 34, 35, 4, 39, 41, 88, 68, 1, 15, 34, 99, 95, 36, 98, 49, 52, 12, 69, 46, 64, 20, 33, 90, 27, 32, 26, 48, 3, 87, 38, 10, 83, 93, 5, 81, 37, 2, 71, 85, 10, 64, 60, 3, 30]