lundi 15 février 2021

Random selection of sample resulting in same sample selection each each time

random.sample() produces the same result each time it is run with or without any seed. The code is

import copy
import sys
import random
import numpy as np
for _ in range(5):

# seedValue = random.randrange(sys.maxsize)
# random.seed(seedValue)
aa = {(10, 11): 1, (10, 12): 1, (11, 10): 2, (11, 12): 2, (12, 10): 2, (12, 11): 2}
aa = {10: 1, 11: 1, 12: 2, 13: 2, 14: 2, 15: 2}

sampleDict_copy = copy.deepcopy(aa)
updated_users = []

while len(updated_users) < 6:

    itemMaxValue = max(sampleDict_copy.items(), key=lambda x: x[1])
    listOfKeys = list() 
    # Iterate over all the items in dictionary to find keys with max value
    for key, value in sampleDict_copy.items():
        if value == itemMaxValue[1]:
            listOfKeys.append(key)


    listOfKeys = np.asarray(listOfKeys)
    remaining_capacity = 6 - len(updated_users)
    if remaining_capacity > 0:
        print(f"remaining_capacity = {remaining_capacity}, listOfKeys = {listOfKeys}")
        if len(listOfKeys) > remaining_capacity: ## listOfKeys can be filled with some combination
            updated_users.extend(random.sample(listOfKeys, remaining_capacity)) ## problem occurring here
        else: ## entire listOfKeys can be entered
            updated_users.extend(listOfKeys)
    
    
    for items in listOfKeys:
        del sampleDict_copy[items] 
print(f"updated_users = {updated_users}")

But I tried with a string as per this answer and here I get different results

import string, random
alphabet = string.ascii_lowercase
print(random.sample( alphabet, 5 ))
# result =  ['x', 'p', 's', 'k', 'h']
print(random.sample( alphabet, 5 ))
# result = ['y', 'h', 'u', 'n', 'd']

In general, where should the seed be provided? My objective is to get a different sample at each time the loop is run.

EDIT: Solution for anyone finding this question in the future

import copy
import sys
import random
import numpy as np
for _ in range(5):

    # seedValue = random.randrange(sys.maxsize)
    # random.seed(seedValue)
    aa = {(10, 11): 1, (10, 12): 1, (11, 10): 2, (11, 12): 2, (12, 10): 2, (12, 11): 2}
    aa = {10: 1, 11: 1, 12: 2, 13: 2, 14: 2, 15: 2}

    sampleDict_copy = copy.deepcopy(aa)
    updated_users = []

    while len(updated_users) < 6:

        itemMaxValue = max(sampleDict_copy.items(), key=lambda x: x[1])
        listOfKeys = list() ## covered UAVs that will be removed once added to the selected_UAVs
        # Iterate over all the items in dictionary to find keys with max value
        for key, value in sampleDict_copy.items():
            if value == itemMaxValue[1]:
                listOfKeys.append(key)

        remaining_capacity = 6 - len(updated_users)
        if remaining_capacity > 0:
            print(f"remaining_capacity = {remaining_capacity}, listOfKeys = {listOfKeys}")
            updated_users.extend(random.sample(listOfKeys, len(listOfKeys)))
        for items in listOfKeys:
            del sampleDict_copy[items] 
    print(f"updated_users = {updated_users}")



Aucun commentaire:

Enregistrer un commentaire