lundi 15 mars 2021

How to ensure that the for loop does not change to next item on list untill if statement is satisfied

I want to have n number of cuboids with uniform distribution. I want to use each volume in this distribution to get height , width and depth within a range. I have written the code below.

import random 
import numpy as np

def cuboidimensions(minwidth,maxwidth , mindepth,maxdepth , minheight,maxheight, numberofcuboids ):
    cuboids = []
    minvolume = minwidth*mindepth*minheight
    maxvolume = maxwidth*maxdepth*maxheight
    cuboidvolume = list(np.random.uniform(minvolume,maxvolume,numberofcuboids))
    print(cuboidvolume)
    volumes = []
    while len(cuboids)<numberofcuboids:
        for i in cuboidvolume:
            width = random.uniform(minwidth,maxwidth)
            #print(roomwidth)
            bywidth = i/width
            height = random.uniform(minheight,maxheight)
            depth = bywidth/height
            if mindepth < depth < maxdepth :
                cuboid = [width , depth , height]
                cuboids.append(cuboid)
                volume = width * depth * height
                volumes.append(volume)
            else:
                #print('break')
                continue
                
       
    return  volumes ,cuboids                  
       
x = cuboidimensions(3,20,3,20,3,7,5) 

print(x)

The output example:

[442.28038126151915, 408.03629466149783, 1414.446813849802, 69.72740444974464, 1834.7672962439801] ([442.2803812615191, 408.03629466149783, 442.28038126151915, 408.0362946614979, 1414.446813849802], [[11.15009197449912, 5.681017397379145, 6.982213208200401], [13.53187840857758, 5.032921637235733, 5.991292736780531], [4.983376476038543, 15.4611798565468, 5.740257111137015], [8.655025551400598, 6.98071480906089, 6.753525517407468], [13.612476509177514, 15.56534197057105, 6.6756077754968945]])

What I want to change is that in the for loop, the same i value from the cuboidvolume should be used till the if condition is met. Like in output, the loop renews and gets 442 again. I do not want that.

What am I doing wrong here and how can I get the required output

Aucun commentaire:

Enregistrer un commentaire