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

Random number generator returning duplicate values

I have a random number generator I want to return 7 keys from a dictionary.

I am using random.uniform to generate a float value which I match to my set of values in my dictionary which are float values to produce the closest value and return the relevant key to that value.

I am iterating over a list to add any unique keys that are produced but ignoring any duplicate keys as I want to end up with a random set of numbers e.g. 5, 12, 6, 18, 28, 23, 15 not duplicate values such as, 5, 12, 5, 15, 23, 12, 5

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

My code to generate the set of numbers works:

count = 0
    while True:
        if count <= 6:
            number = ran.uniform(0.0, max_key)
            num_gen = min(sorted_countout, key=lambda x:abs(x-(number)))
            snumgen = list()
            if num_gen not in snumgen:
                snumgen.append(num_gen)
                count += 1
            else:
                count += 0
            print(snumgen)
        elif count > 6:
            break

However it isn’t producing a unique set of numbers, I am still getting duplicate values in my produced set.

Also as a separate issue- how would I reset count outside of the loop? it completes the loop but then locks at 6- I have to close vscode to get it to generate a new set of numbers-

NOTE: There are some similar questions in this vein of questioning- but I am more specifically asking for a solution in this context as it should be producing unique numbers but it isn’t currently. I am a junior dev and appreciate your help as it helps me learn!

##Here is an example of my dictionary:

sorted_countout = {1: 17.8343949044586, 2: 21.337579617834397, 3: 21.656050955414013, 4: 19.745222929936308, 5: 19.10828025477707, 6: 20.382165605095544, 7: 23.88535031847134, 8: 17.8343949044586, 9: 21.97452229299363, 10: 20.382165605095}
import random as ran

##this is the function
def choices():
    ##selecting the maximum value in the dict
    max_key = next(iter(sorted_countout))
    for key in sorted_countout:
        if sorted_countout[key] > sorted_countout[max_key]:
            max_key = key
    ##Randomly select a float value in range of 0- max freq of occurrence
    
    count = 0
    while True:
        if count <= 6:
            number = ran.uniform(0.0, max_key)
            num_gen = min(sorted_countout, key=lambda x:abs(x-(number)))
            snumgen = list()
            if num_gen not in snumgen:
                snumgen.append(num_gen)
                count += 1
            else:
                count += 0
            print(snumgen)
        elif count > 6:
            break

>Solution :

That’s because you’re instantiating your list inside the loop, so you reset it every cycle. Also, you need to set your counter back to 0 right after the loop:

def choices():
    max_key = next(iter(sorted_countout))
    for key in sorted_countout:
        if sorted_countout[key] > sorted_countout[max_key]:
            max_key = key

    count = 0
    snumgen = []  # Move the list initialization outside the loop
    while True:
        if count <= 6:
            number = ran.uniform(0.0, max_key)
            num_gen = min(sorted_countout, key=lambda x:abs(x-(number)))
            if num_gen not in snumgen:
                snumgen.append(num_gen)
                count += 1
            else:
                continue  # Skip duplicate numbers
        else:
            break

    print(snumgen)
    count = 0  # Reset count outside the loop
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