Search code examples
pythonfunctionrandom

Non-Repeating Random Numbers for Multiple Variables


I am trying to create a function that applies random values to a range of parameters that are used in another function where the random sequence does not repeat.

The reason: used for random hyper parameter tuning and to cut down on processing by not repeating sequence.

Example Code:


num_evals = 2500

parameters = {
        'n_parameter_1': range(2,100),
        'n_parameter_2': range(1,20),
        'n_parameter_3': range(2,150),
              }  

for i in range(num_evals):
        n_parameter_1 = random.choice(parameters['n_parameter_1'])
        n_parameter_2 = random.choice(parameters['n_parameter_2'])
        n_parameter_3 = random.choice(parameters['n_parameter_3'])

The results then get populated to a file with the random parameters used to generate.

I need help with the second bit.

As this function runs over time, you start getting sequences that look like this:

n_parameter_1 = 54, n_parameter_2 = 15, n_parameter_3 = 120

n_parameter_1 = 10, n_parameter_2 = 12, n_parameter_3 = 89

n_parameter_1 = 54, n_parameter_2 = 15, n_parameter_3 = 120

I want to avoid the last sequence by either capturing the sequences in a list or dataframe etc. (already saving the sequences) that the function checks and then generates a new/different sequence if there is a duplicate sequence in the list.

Thanks for the help in advance.


Solution

  • You can store sequences in a set and then check if a sequence is already in the set.

    num_evals = 2500
    i = 0
    
    parameters = {
            'n_parameter_1': range(2,100),
            'n_parameter_2': range(1,20),
            'n_parameter_3': range(2,150),
                  }  
    
    sequences = set()
    
    while i < num_evals:
            n_parameter_1 = random.choice(parameters['n_parameter_1'])
            n_parameter_2 = random.choice(parameters['n_parameter_2'])
            n_parameter_3 = random.choice(parameters['n_parameter_3'])
            sequence = (n_parameter_1, n_parameter_2, n_parameter_3)
            if sequence not in sequences:
                sequences.add(sequence)
                i += 1
    

    The for loop has been changed to a while loop to allow for the conditional increment.