Home > Net >  Creation of list with random value pairs
Creation of list with random value pairs

Time:09-22

I want to create a list with 500 or more value pairs with the following outcome and some boundaries, but I stuck a lot with the correct syntax.

The outcome should look like following:

SiO2 = 35, Al2O3 = 12, CaO = 41, MgO = 12, T = 1498
SiO2 = 38, Al2O3 = 7, CaO = 46, MgO = 9, T = 1512
...

and so on.

I got the following code snippet so far:

from random import randint

CaO = randint(34, 40)
SiO2= randint(30, 40)
MgO = randint(5, 15)
Al2O3 = randint(5, 15)
T = randint(1400, 1550)

liste = []

for i in range(1000):
    if not CaO   SiO2   MgO   Al2O3 == 100:
        continue
    elif CaO / SiO2 > 1.3 & CaO / SiO2 < 0.85:
        continue
    elif (CaO   MgO) / (SiO2   Al2O3) < 0.84 & (CaO   MgO) / (SiO2   Al2O3) > 1.25:
        continue
    else: 
        liste.append(CaO, SiO2, MgO, Al2O3, T)
        
    print(liste)

If anybody could give me some hints it would be great.

Cheers

CodePudding user response:

Instead of a hit or miss approach which misses 97% of the time, you could precompute valid 4-tuples that satisfy your conditions ahead of time, select from them, and then tack on the unconstrained 5th component.

Something like this:

from random import randint,choice
from itertools import product

def valid(p):
    CaO, SiO2, MgO, Al2O3 = p
    conditions = (CaO   SiO2   MgO   Al2O3 == 100,
                  CaO / SiO2 <= 1.3 or CaO / SiO2 >= 0.85,
                  (CaO   MgO) / (SiO2   Al2O3) >= 0.84 or (CaO   MgO) / (SiO2   Al2O3) <= 1.25)
    return all(conditions)
                    
valids = [p for p in product(range(34,41),range(30,41),range(5,16),range(5,16)) if valid(p)]

liste = [choice(valids)  (randint(1400, 1550),) for _ in range(1000)]
print(liste[:5])

Typical output:

[(36, 35, 14, 15, 1485), (40, 34, 14, 12, 1447), (39, 39, 11, 11, 1401), (39, 34, 15, 12, 1510), (40, 32, 14, 14, 1462)]

CodePudding user response:

It is not really clear what you are trying to do but I'm under the impression the following code snippet result is close enough. You need to double-check the conditions though.

from random import randint
from pprint import pprint

pairs = []
labels = ('Ca0', 'SiO2', 'MgO', 'Al2O3', 'T')

for _ in range(1000):
    CaO = randint(34, 40)
    SiO2 = randint(30, 40)
    MgO = randint(5, 15)
    Al2O3 = randint(5, 15)
    T = randint(1400, 1550)

    if all((
            CaO   SiO2   MgO   Al2O3 == 100,
            0.85 <= CaO / SiO2 <= 1.3,
            0.84 <= (CaO   MgO) / (SiO2   Al2O3) <= 1.25)):
        pairs.append(dict(
            zip(labels, (CaO, SiO2, MgO, Al2O3, T))
        ))

pprint(pairs)

And it will print something like this:

[{'Al2O3': 9, 'Ca0': 39, 'MgO': 15, 'SiO2': 37, 'T': 1498},
 {'Al2O3': 10, 'Ca0': 39, 'MgO': 13, 'SiO2': 38, 'T': 1426},
 {'Al2O3': 15, 'Ca0': 37, 'MgO': 10, 'SiO2': 38, 'T': 1449},
 {'Al2O3': 12, 'Ca0': 39, 'MgO': 10, 'SiO2': 39, 'T': 1500},
 {'Al2O3': 10, 'Ca0': 39, 'MgO': 15, 'SiO2': 36, 'T': 1473},
 {'Al2O3': 15, 'Ca0': 37, 'MgO': 11, 'SiO2': 37, 'T': 1534},
 {'Al2O3': 14, 'Ca0': 39, 'MgO': 13, 'SiO2': 34, 'T': 1475},
 {'Al2O3': 11, 'Ca0': 36, 'MgO': 15, 'SiO2': 38, 'T': 1412},
 {'Al2O3': 14, 'Ca0': 37, 'MgO': 10, 'SiO2': 39, 'T': 1425},
 {'Al2O3': 12, 'Ca0': 40, 'MgO': 9, 'SiO2': 39, 'T': 1471},
 {'Al2O3': 14, 'Ca0': 35, 'MgO': 14, 'SiO2': 37, 'T': 1486},
 {'Al2O3': 14, 'Ca0': 35, 'MgO': 12, 'SiO2': 39, 'T': 1466},
 {'Al2O3': 15, 'Ca0': 38, 'MgO': 8, 'SiO2': 39, 'T': 1478},
 {'Al2O3': 11, 'Ca0': 38, 'MgO': 12, 'SiO2': 39, 'T': 1532},
 {'Al2O3': 11, 'Ca0': 39, 'MgO': 14, 'SiO2': 36, 'T': 1425},
 {'Al2O3': 9, 'Ca0': 40, 'MgO': 14, 'SiO2': 37, 'T': 1511},
 {'Al2O3': 13, 'Ca0': 34, 'MgO': 15, 'SiO2': 38, 'T': 1509},
 {'Al2O3': 12, 'Ca0': 38, 'MgO': 10, 'SiO2': 40, 'T': 1444},
 {'Al2O3': 11, 'Ca0': 36, 'MgO': 15, 'SiO2': 38, 'T': 1419},
 {'Al2O3': 15, 'Ca0': 38, 'MgO': 8, 'SiO2': 39, 'T': 1546},
 {'Al2O3': 14, 'Ca0': 40, 'MgO': 6, 'SiO2': 40, 'T': 1461},
 {'Al2O3': 12, 'Ca0': 40, 'MgO': 10, 'SiO2': 38, 'T': 1445},
 {'Al2O3': 12, 'Ca0': 39, 'MgO': 9, 'SiO2': 40, 'T': 1492},
 {'Al2O3': 12, 'Ca0': 38, 'MgO': 10, 'SiO2': 40, 'T': 1530},
 {'Al2O3': 15, 'Ca0': 36, 'MgO': 14, 'SiO2': 35, 'T': 1500},
 {'Al2O3': 8, 'Ca0': 38, 'MgO': 14, 'SiO2': 40, 'T': 1426},
 {'Al2O3': 12, 'Ca0': 39, 'MgO': 12, 'SiO2': 37, 'T': 1543},
 {'Al2O3': 12, 'Ca0': 40, 'MgO': 11, 'SiO2': 37, 'T': 1533},
 {'Al2O3': 13, 'Ca0': 36, 'MgO': 12, 'SiO2': 39, 'T': 1428}]
  • Related