If you generate the random numbers one at a time, you can just keep track of whether they increase the max or min values. You will still have to compute the values, but you won't run into a memory issue since you only have to store three numbers (max, min, and latest_random)
import numpy as np
max_=0
min_=0
for i in range(1000):
new_number=np.random.normal(0,1,1)
if new_number>max_:
max_=new_number
if new_number<min_:
min_=new_number
range_=max_-min_
print(range_)
To speed up the computation you can do larger blocks at a time. If you want to do a run with a billion numbers, you can calculate a million at a time and run the loop a thousand times. Modified code and time results below
import numpy as np
import time
max_=0
min_=0
start=time.time()
for i in range(1000):
new_array=np.random.normal(0,1,1000000)
new_max=np.max(new_array)
new_min=np.min(new_array)
if new_max>max_:
max_=new_max
if new_min<min_:
min_=new_min
range_=max_-min_
print('Range ', range_)
end = time.time()
Time=end - start
print('Time ',Time)
Range 12.421138327443614
Time 36.7797749042511
Comparing the results of running one random number at a time vs. ten at a time to see if results are significantly different
(each one run three times)
One at a time:
new_numbers=[]
for i in range(10):
new_numbers.append(np.random.normal(0,1,1)[0])
print(new_numbers)
[-1.0145267697638918, -1.1291506481372602, 1.3622608858856742, 0.16024562390261188, 1.062550043104352, -0.4160329548439351, -0.05464203711515494, -0.7416629430695286, 0.35066071936940363, 0.06498345663995017]
[-1.5632632129838873, -1.0314300796946991, 0.5014408178125339, -0.37806631815396563, 0.45396918178048334, -0.6630479858064194, -0.47097483551189306, 0.40734077106402056, 1.1167819302886144, -0.6594075991871857]
[0.4448783416507262, 0.20160041940565818, -0.4781753245124433, -0.7130750653981222, -0.8035305391034386, -0.41543648761183466, 0.25166027175788847, -0.7051417978559822, 0.6017351178904993, -1.3719596304190458]
Ten at a time:
np.random.normal(0,1,10)
array([-1.79498658, 0.89073416, -0.25302627, -0.17237986, -0.38988131,
-0.93635678, 0.28824899, 0.52675642, 0.86195635, -0.89584341])
array([ 1.41602405, 1.33800937, 1.87837334, 0.2082182 , -0.25116545,
1.37953259, 0.34445565, -0.33647043, -0.24414261, -0.14505838])
array([ 0.43848371, -0.60967936, 1.2902231 , 0.44589728, -2.39725248,
-1.42715386, -1.0627627 , 1.15998483, 0.96427742, -2.01062938])