Passed
Push — master ( 0f0b8d...36e873 )
by Simon
01:29
created

gradient_free_optimizers.utils.set_random_seed()   A

Complexity

Conditions 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nop 2
dl 13
loc 13
rs 10
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import random
6
import numpy as np
7
8
9 View Code Duplication
def set_random_seed(nth_process, random_state):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
10
    """
11
    Sets the random seed separately for each thread
12
    (to avoid getting the same results in each thread)
13
    """
14
    if nth_process is None:
15
        nth_process = 0
16
17
    if random_state is None:
18
        random_state = np.random.randint(0, high=2 ** 31 - 2, dtype=np.int64)
19
20
    random.seed(random_state + nth_process)
21
    np.random.seed(random_state + nth_process)
22
23
24
def move_random(ss_positions):
25
    position = []
26
    for search_space_pos in ss_positions:
27
        pos_ = random.choice(search_space_pos)
28
        position.append(pos_)
29
30
    return np.array(position)
31