mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-23 05:48:34 +03:00
saip/param_source: try to not repeat random values
Change-Id: I4fa743ef5677580f94b9df16a5051d1d178edeb0
This commit is contained in:
@@ -100,6 +100,7 @@ class RandomDigitSource(InputExpandingParamSource, RandomSourceMixin):
|
|||||||
'return a different sequence of random decimal digits each'
|
'return a different sequence of random decimal digits each'
|
||||||
is_abstract = False
|
is_abstract = False
|
||||||
name = 'random decimal digits'
|
name = 'random decimal digits'
|
||||||
|
used_keys = set()
|
||||||
|
|
||||||
def __init__(self, num_digits, first_value, last_value):
|
def __init__(self, num_digits, first_value, last_value):
|
||||||
"""
|
"""
|
||||||
@@ -118,7 +119,16 @@ class RandomDigitSource(InputExpandingParamSource, RandomSourceMixin):
|
|||||||
self.val_first_last = (first_value, last_value)
|
self.val_first_last = (first_value, last_value)
|
||||||
|
|
||||||
def get_next(self, csv_row:dict=None):
|
def get_next(self, csv_row:dict=None):
|
||||||
val = self.random_impl.randint(*self.val_first_last)
|
# try to generate random digits that are always different from previously produced random bytes
|
||||||
|
attempts = 10
|
||||||
|
while True:
|
||||||
|
val = self.random_impl.randint(*self.val_first_last)
|
||||||
|
if val in RandomDigitSource.used_keys:
|
||||||
|
attempts -= 1
|
||||||
|
if attempts:
|
||||||
|
continue
|
||||||
|
RandomDigitSource.used_keys.add(val)
|
||||||
|
break
|
||||||
return self.val_to_digit(val)
|
return self.val_to_digit(val)
|
||||||
|
|
||||||
def val_to_digit(self, val:int):
|
def val_to_digit(self, val:int):
|
||||||
@@ -144,6 +154,7 @@ class RandomHexDigitSource(InputExpandingParamSource, RandomSourceMixin):
|
|||||||
'return a different sequence of random hexadecimal digits each'
|
'return a different sequence of random hexadecimal digits each'
|
||||||
is_abstract = False
|
is_abstract = False
|
||||||
name = 'random hexadecimal digits'
|
name = 'random hexadecimal digits'
|
||||||
|
used_keys = set()
|
||||||
|
|
||||||
def __init__(self, num_digits):
|
def __init__(self, num_digits):
|
||||||
'see from_str()'
|
'see from_str()'
|
||||||
@@ -156,7 +167,17 @@ class RandomHexDigitSource(InputExpandingParamSource, RandomSourceMixin):
|
|||||||
self.num_digits = num_digits
|
self.num_digits = num_digits
|
||||||
|
|
||||||
def get_next(self, csv_row:dict=None):
|
def get_next(self, csv_row:dict=None):
|
||||||
val = self.random_impl.randbytes(self.num_digits // 2)
|
# try to generate random bytes that are always different from previously produced random bytes
|
||||||
|
attempts = 10
|
||||||
|
while True:
|
||||||
|
val = self.random_impl.randbytes(self.num_digits // 2)
|
||||||
|
if val in RandomHexDigitSource.used_keys:
|
||||||
|
attempts -= 1
|
||||||
|
if attempts:
|
||||||
|
continue
|
||||||
|
RandomHexDigitSource.used_keys.add(val)
|
||||||
|
break
|
||||||
|
|
||||||
return b2h(val)
|
return b2h(val)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user