Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Save while loop results as numpy array in python?

Fairly new at coding, Im using python to do a monte carlo simulation. Not really sure how I would go about this, the end result would be to graph out the results from the steadystate function.

import random
import numpy as np

def steadystate():
    p=0.88
    Cout=4700000000
    LambdaAER=0.72
    Vol=44.5
    Depo=0.42
    Uptime=0.1
    Effic=0.38
    Recirc=4.3
    x = random.randint(86900000,2230000000000)
    conc = ((p*Cout*LambdaAER)+(x/Vol))/(LambdaAER+Depo+(Uptime*Effic*Recirc))
    return conc

x = 0
while x < 10000:
    result = steadystate()
    print(result)
    x+=1

Right now I just want to save the results from the while loop as an array so I can graph it. Ive tried appending it but it wouldnt print anything.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The simplest way to achieve what you need would be to generate your random integers as an array to start with, e.g.:

size = 10000

# generate 10000 (size) random integers as a numpy array
x = np.random.randint(86900000, 2230000000000, size=size)

p = 0.88
Cout = 4700000000
LambdaAER = 0.72
Vol = 44.5
Depo = 0.42
Uptime = 0.1
Effic = 0.38
Recirc = 4.3

result = ((p*Cout*LambdaAER)+(x/Vol))/(LambdaAER+Depo+(Uptime*Effic*Recirc))

The result object will also be a numpy array of the same size as x.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading