vendredi 8 novembre 2019

Add random integer value to elements in list, but sum of list must not change

I try to randomize some data in python, with following formula:

limit = 10 # value bewteen 0 and 100, const. for all values on the dataset
for old in objects:
    new_v = old + old * int(math.ceil(random_limit(limit))
    new_objects.append(new_v)

def random_limit(limit):
    # create value between [-1; 1)
    rand = np.random.rand()*2.0 - 1.0; 
    return ((rand * float(limit)) / 100.0)

The idea is, to set a procentual limit for the randomizing on the initial old_value. This limit has to be the same for all data.

Now it would be nice if could achieve sum(objects) == sum(new_objects). I don't have any idea how to start and if this is even possible under the limit condition. Any ideas?




Aucun commentaire:

Enregistrer un commentaire