44
55import random
66from enum import IntEnum
7- from typing import Sequence , Union , Tuple , List , Set
7+ from typing import Optional , Sequence , TypeVar , Union , Tuple , List , Set
88from typing import cast as typecast
99
1010from .utils import list_like
@@ -16,7 +16,8 @@ class VectorRandomMode(IntEnum):
1616 float = 2
1717
1818
19- _Number = Union [int , float ]
19+ _Number = TypeVar ('_Number' , int , float )
20+ _Range = List [Union [_Number , Tuple [_Number , _Number ]]]
2021
2122
2223class Vector :
@@ -28,17 +29,16 @@ class Vector:
2829 @staticmethod
2930 def random (
3031 num : int = 5 ,
31- position_range : Union [List [Union [_Number , Tuple [_Number , _Number ]]],
32- None ] = None ,
32+ position_range : Optional [_Range [Union [int , float ]]] = None ,
3333 mode : VectorRandomMode = VectorRandomMode .unique ,
3434 ) -> Union [IntVector , FloatVector ]:
3535 """
3636 Generate `num` random vectors in limited space
3737 Args:
38- num: the number of vectors
38+ num: the length of vectors
3939 position_range: a list of limits for each dimension
40- single number x represents range ( 0, x)
41- list [x, y] or tuple (x, y) represents range ( x, y)
40+ single number x represents range [ 0, x]
41+ list [x, y] or tuple (x, y) represents range [ x, y]
4242 mode: the mode vectors generate, see Enum Class VectorRandomMode
4343 """
4444 if position_range is None :
@@ -49,8 +49,8 @@ def random(
4949
5050 dimension = len (position_range )
5151
52- offset : Sequence [_Number ] = []
53- length : Sequence [_Number ] = []
52+ offset : Sequence [Union [ int , float ] ] = []
53+ length : Sequence [Union [ int , float ] ] = []
5454
5555 vector_space = 1
5656 for i in range (0 , dimension ):
@@ -128,9 +128,67 @@ def get_vector(dimension: int, position_range: Sequence[int],
128128 Returns:
129129 list: A list representing the generated vector.
130130 """
131-
132131 tmp : List [int ] = []
133132 for i in range (0 , dimension ):
134133 tmp .append (hashcode % (position_range [i ] + 1 ))
135134 hashcode //= (position_range [i ] + 1 )
136135 return tmp
136+
137+ @staticmethod
138+ def random_unique_vector (num : int = 5 ,
139+ position_range : Union [_Range [int ], None ] = None ):
140+ """
141+ Generate `num` unique vectors with specified parameters. It is a wrapper for Vector.random.
142+
143+ Args:
144+ num: the length of vectors
145+ position_range: a list of limits for each dimension
146+ single number x represents range [0, x]
147+ list [x, y] or tuple (x, y) represents range [x, y]
148+ """
149+ return typecast (
150+ Vector .IntVector ,
151+ Vector .random (
152+ num ,
153+ typecast (Optional [_Range [Union [int , float ]]], position_range ),
154+ VectorRandomMode .unique ))
155+
156+ @staticmethod
157+ def random_repeatable_vector (
158+ num : int = 5 ,
159+ position_range : Optional [List [Union [int , Tuple [int ,
160+ int ]]]] = None ):
161+ """
162+ Generate `num` repeatable vectors with specified parameters.
163+ It is a wrapper for Vector.random.
164+
165+ Args:
166+ num: the length of vectors
167+ position_range: a list of limits for each dimension
168+ single number x represents range [0, x]
169+ list [x, y] or tuple (x, y) represents range [x, y]
170+ """
171+ return typecast (
172+ Vector .IntVector ,
173+ Vector .random (
174+ num ,
175+ typecast (Optional [_Range [Union [int , float ]]], position_range ),
176+ VectorRandomMode .repeatable ))
177+
178+ @staticmethod
179+ def random_float_vector (
180+ num : int = 5 ,
181+ position_range : Optional [_Range [Union [int , float ]]] = None ):
182+ """
183+ Generate `num` float vectors with specified parameters.
184+ It is a wrapper for Vector.random.
185+
186+ Args:
187+ num: the length of vectors
188+ position_range: a list of limits for each dimension
189+ single number x represents range [0, x]
190+ list [x, y] or tuple (x, y) represents range [x, y]
191+ """
192+ return typecast (
193+ Vector .FloatVector ,
194+ Vector .random (num , (position_range ), VectorRandomMode .float ))
0 commit comments