jeudi 22 octobre 2020

Generating unique random list with conditions

I recently started to play around with the random module in Python but came across a problem that for whatever reason I cannot seem to solve entirely.

I've been trying to write a program that creates a list of random numbers such that each number is unique and not divisible by the numbers that come before it.

My initial attempt to solve this problem was to break it down into two functions, first a function that takes a list and outputs a single integer that is unique and not divisible by any of the integers in the input list. Then I would make a second function that would make a list of length N based on the previous function.

So I could call the function make_rand_list(5) and the output could be [2, 9, 17, 37, 53].
Here is what I have attempted so far and I am definitely headed in the wrong direction:

import random 


def gen_number(l):
    rand_num = random.randint(1, 99)
    
    #all(rand_num % i == 0 for item in l) ...(not sure how I would use this?)
    
    #if rand_num not in l and rand_num ...(idk?)
    
    for num in l: 
        if rand_num != num and rand_num % num != 0:
            gen_num = rand_num 
        else:
            gen_num = 0
    
    return gen_num
            
def make_rand_list(N):
    pass

Also, how would I go about counting the amount of times the library function to generate a random number has been called? Would appreciate any guidance on this, thanks!




Aucun commentaire:

Enregistrer un commentaire