如何高效生成特定范围内的唯一随机非零整数?

huangapple go评论161阅读模式
英文:

How to efficiently generate unique random non-zero integers from specific spaces in a range?

问题

以下是您要翻译的代码部分:

  1. import random
  2. my_list = []
  3. c = 0
  4. while c < 100:
  5. if c < 5:
  6. # Both values are unique.
  7. first_num, second_num = random.sample(range(-5, 6), 2)
  8. # Exclusion of 0.
  9. while first_num == 0 or second_num == 0:
  10. first_num, second_num = random.sample(range(-5, 6), 2)
  11. c += 1
  12. elif 5 <= c < 80:
  13. first_num, second_num = random.sample(range(-100, 101), 2)
  14. while first_num == 0 or second_num == 0:
  15. first_num, second_num = random.sample(range(-100, 101), 2)
  16. c += 1
  17. else:
  18. first_num, second_num = random.sample(range(-150, 151), 2)
  19. while first_num == 0 or second_num == 0:
  20. first_num, second_num = random.sample(range(-150, 151), 2)
  21. c += 1
  22. random_nums = (first_num, second_num)
  23. if random_nums not in my_list:
  24. my_list.append(random_nums)
  25. else:
  26. c -= 1

如果您有任何其他需要翻译的内容,请随时提出。

英文:

I want to generate 100 pairs of unique random non-zero integers from the range (-150, 151). But I want my code to generate from specific areas in (-150, 151). I have coded it as follows:

  1. import random
  2. my_list = []
  3. c = 0
  4. while c &lt; 100:
  5. if c &lt; 5:
  6. # Both values are unique.
  7. first_num, second_num = random.sample(range(-5, 6), 2)
  8. # Exclusion of 0.
  9. while first_num == 0 or second_num == 0:
  10. first_num, second_num = random.sample(range(-5, 6), 2)
  11. c += 1
  12. elif 5 &lt;= c &lt; 80:
  13. first_num, second_num = random.sample(range(-100, 101), 2)
  14. while first_num == 0 or second_num == 0:
  15. first_num, second_num = random.sample(range(-100, 101), 2)
  16. c += 1
  17. else:
  18. first_num, second_num = random.sample(range(-150, 151), 2)
  19. while first_num == 0 or second_num == 0:
  20. first_num, second_num = random.sample(range(-150, 151), 2)
  21. c += 1
  22. random_nums = (first_num, second_num)
  23. if random_nums not in my_list:
  24. my_list.append(random_nums)
  25. else:
  26. c -= 1

Is there a more elegant or efficient way alternative to what I have coded above?

UPDATE

After discussing with MatBailie in the comments, I am updating the requirements:

If the whole selected range is (-150 to 151), then in the final my_list:

  • (5,6), (6,5) are allowed but (5,6), (5,6) are not.
  • If (-1, 1) is generated once from range (-5 to 6) then it should not be generated again from the bigger ranges such as (-100 to 101) or (-150 to 151)
  • It is also good to have pairs such as (-1, 140) or (140, -1).

UPDATE

Benchmarking MatBailie, Alain T. and Samwise's answers on perfpy with 100 pairs, I got the following result:

如何高效生成特定范围内的唯一随机非零整数?

The same benchmark with generating 1000 pairs and the same ratio of areas, I got this:

如何高效生成特定范围内的唯一随机非零整数?

答案1

得分: 2

以下是代码的中文翻译:

  1. # 可以构建一个包含100个范围的列表,以满足所需大小的分组。这将使剩下的逻辑更加通用和简单:
  2. import random
  3. ranges = [range(-5,6)]*5 + [range(-100,101)]*75 + [range(-150,151)]*20
  4. pairs = []
  5. for R in ranges:
  6. a = 0
  7. while not a or not b or (a,b) in pairs:
  8. a,b = random.sample(R,2) # random.choices(R,k=2)
  9. pairs.append((a,b))
  10. # 输出:
  11. print(pairs)
  12. # [(-3, 2), (-3, -5), (3, 1), (-4, -3), (2, 4), (-73, -9), (16, -56),
  13. # (12, -81), (5, -50), (99, -6), (35, 71), (30, -75), (98, 25), (55, -58),
  14. # (73, -24), (-65, 4), (75, -96), (-6, -90), (-80, 22), (-93, -39),
  15. # (-69, -48), (-30, 25), (85, -11), (37, 60), (91, 96), (98, 100),
  16. # (-100, -54), (58, 20), (-14, -95), (-76, -12), (-5, -84), (70, -53),
  17. # (91, -66), (-61, 2), (4, -42), (-15, -70), (-52, -6), (-5, 93), (26, 76),
  18. # (-8, -79), (63, -7), (-23, -27), (56, 13), (46, 27), (80, 94), (-94, -66),
  19. # (-46, -19), (-4, -87), (-92, -48), (24, 32), (10, -89), (-50, -96),
  20. # (-5, -85), (-32, 69), (-60, 69), (87, -81), (-100, -91), (87, 37),
  21. # (-60, 45), (-70, -84), (-98, 80), (-88, 57), (-67, -44), (-72, 4),
  22. # (-76, 5), (-79, -16), (-9, 80), (76, -41), (15, 77), (-47, -1), (58, 12),
  23. # (66, -2), (21, 16), (-24, -12), (54, -69), (58, -73), (61, 68), (-89, -37),
  24. # (-85, 68), (-7, -21), (-86, -31), (48, 136), (137, 86), (-61, -32),
  25. # (104, -144), (-24, -103), (80, 135), (-2, 39), (49, -21), (15, -103),
  26. # (-14, -145), (-48, -61), (142, -90), (93, -132), (-68, -111), (-80, 2),
  27. # (6, 45), (-89, 75), (-146, -76), (-94, -24)]
  28. # 你也可以只使用一个列表来实现,通过初始化包含范围的结果列表,然后用生成的对替换范围:
  29. pairs = [range(-5,6)]*5 + [range(-100,101)]*75 + [range(-150,151)]*20
  30. for i,R in enumerate(pairs):
  31. pairs[i] = (0,0)
  32. while not all(pairs[i]) or pairs.index(pairs[i])&lt;i:
  33. pairs[i] = random.sample(R,2) # random.choices(R,k=2)

注意:请确保在代码中不要使用HTML实体"lt",而应使用小于号"<"。

英文:

You could build a list of the 100 ranges repeating the same ones in groups of the required size. This will make the rest of the logic more generalized and simpler:

  1. import random
  2. ranges = [range(-5,6)]*5 + [range(-100,101)]*75 + [range(-150,151)]*20
  3. pairs = []
  4. for R in ranges:
  5. a = 0
  6. while not a or not b or (a,b) in pairs:
  7. a,b = random.sample(R,2) # random.choices(R,k=2)
  8. pairs.append((a,b))

output:

  1. print(pairs)
  2. [(-3, 2), (-3, -5), (3, 1), (-4, -3), (2, 4), (-73, -9), (16, -56),
  3. (12, -81), (5, -50), (99, -6), (35, 71), (30, -75), (98, 25), (55, -58),
  4. (73, -24), (-65, 4), (75, -96), (-6, -90), (-80, 22), (-93, -39),
  5. (-69, -48), (-30, 25), (85, -11), (37, 60), (91, 96), (98, 100),
  6. (-100, -54), (58, 20), (-14, -95), (-76, -12), (-5, -84), (70, -53),
  7. (91, -66), (-61, 2), (4, -42), (-15, -70), (-52, -6), (-5, 93), (26, 76),
  8. (-8, -79), (63, -7), (-23, -27), (56, 13), (46, 27), (80, 94), (-94, -66),
  9. (-46, -19), (-4, -87), (-92, -48), (24, 32), (10, -89), (-50, -96),
  10. (-5, -85), (-32, 69), (-60, 69), (87, -81), (-100, -91), (87, 37),
  11. (-60, 45), (-70, -84), (-98, 80), (-88, 57), (-67, -44), (-72, 4),
  12. (-76, 5), (-79, -16), (-9, 80), (76, -41), (15, 77), (-47, -1), (58, 12),
  13. (66, -2), (21, 16), (-24, -12), (54, -69), (58, -73), (61, 68), (-89, -37),
  14. (-85, 68), (-7, -21), (-86, -31), (48, 136), (137, 86), (-61, -32),
  15. (104, -144), (-24, -103), (80, 135), (-2, 39), (49, -21), (15, -103),
  16. (-14, -145), (-48, -61), (142, -90), (93, -132), (-68, -111), (-80, 2),
  17. (6, 45), (-89, 75), (-146, -76), (-94, -24)]

You could also do this using only one list by initializing the resulting list with ranges that you replace with the generated pairs:

  1. pairs = [range(-5,6)]*5 + [range(-100,101)]*75 + [range(-150,151)]*20
  2. for i,R in enumerate(pairs):
  3. pairs[i] = (0,0)
  4. while not all(pairs[i]) or pairs.index(pairs[i])&lt;i:
  5. pairs[i] = random.sample(R,2) # random.choices(R,k=2)

答案2

得分: 2

这是你要翻译的内容:

"已经过了午夜,但我将尽力解释...

基本原则是我要计算在给定范围内(不包括零)有多少不同的配对,然后在该范围上运行一个样本,以确保没有重复项。

然后,我编写一个函数来“解码”这些整数为配对。

所以,如果我想要一个元素在范围-4到+4之间的配对,不包括零,不包括范围-2到+2的配对,我可以将其映射到可能配对的网格...

  1. -4 -3 -2 -1 +0 +1 +2 +3 +4
  2. +4 O O O O X O O O O
  3. +3 O O O O X O O O O
  4. +2 O O X X X X X O O
  5. +1 O O X X X X X O O
  6. +0 X X X X X X X X X
  7. -1 O O X X X X X O O
  8. -2 O O X X X X X O O
  9. -3 O O O O X O O O O
  10. -4 O O O O X O O O O

O = 可能的配对
X = 排除的配对

有48种允许的可能配对。

  • 48 = (4² - 2²) * 4

然后,我编写一个函数,可以将值0..47中的每个值转换为这些允许的可能配对之一。

  1. import random
  2. from pprint import pprint
  3. def decode(val, lower, upper):
  4. q = val % 4
  5. v = val >> 2
  6. x = v % (upper + lower) - lower
  7. if x >= 0:
  8. x += 1
  9. y = v // (upper + lower) + lower + 1
  10. if q in (1,3):
  11. x = -x
  12. y = -y
  13. if q in (2,3):
  14. x,y = -y,x
  15. return (x,y)
  16. def pairs(n, lower, upper):
  17. return [
  18. decode(id, lower, upper)
  19. for id in random.sample(
  20. range((upper**2 - lower**2) * 4),
  21. n
  22. )
  23. ]
  24. # 较小的范围用于使其可打印和可测试
  25. result = [
  26. *pairs( 5, 0, 5), # 在范围-5到5的情况下有5对,不包括范围-0到0的情况
  27. *pairs( 5, 5, 10), # 在范围-10到10的情况下有5对,不包括范围-5到5的情况
  28. *pairs( 5, 10, 20) # 在范围-20到20的情况下有5对,不包括范围-10到10的情况
  29. ]
  30. pprint(result)

演示:https://trinket.io/python3/f9e65fe9ef"

英文:

It's gone past midnight, but I'll do my best to explain...

The basic principle is that I work out how many different pairs there are in a given range (excluding any zeros), and run a sample on that range, so I know there are no duplicates.

Then I code a function to "decode" those integers in to a pairs.

So, if I want a pair with elements in the range -4 to +4, excluding zeros, excluding pairs from the range -2 to +2, I can map that to a grid of possible pairs...

  1. -4 -3 -2 -1 +0 +1 +2 +3 +4
  2. +4 O O O O X O O O O
  3. +3 O O O O X O O O O
  4. +2 O O X X X X X O O
  5. +1 O O X X X X X O O
  6. +0 X X X X X X X X X
  7. -1 O O X X X X X O O
  8. -2 O O X X X X X O O
  9. -3 O O O O X O O O O
  10. -4 O O O O X O O O O

O = Possible pair
X = Excluded pair

There are 48 allowed possible pairs.

  • 48 = (4² - 2²) * 4

I then write a function that can turn each of the values 0..47 in to a different one of those allowed possible pairs.

  1. import random
  2. from pprint import pprint
  3. def decode(val, lower, upper):
  4. q = val % 4
  5. v = val &gt;&gt; 2
  6. x = v % (upper + lower) - lower
  7. if x &gt;= 0:
  8. x += 1
  9. y = v // (upper + lower) + lower + 1
  10. if q in (1,3):
  11. x = -x
  12. y = -y
  13. if q in (2,3):
  14. x,y = -y,x
  15. return (x,y)
  16. def pairs(n, lower, upper):
  17. return [
  18. decode(id, lower, upper)
  19. for id in random.sample(
  20. range((upper**2 - lower**2) * 4),
  21. n
  22. )
  23. ]
  24. # smaller ranges used to make it printable and testable
  25. result = [
  26. *pairs( 5, 0, 5), # 5 pairs with values in the range - 5 to 5, except pairs using the range - 0 to 0
  27. *pairs( 5, 5, 10), # 5 pairs with values in the range -10 to 10, except pairs using the range - 5 to 5
  28. *pairs( 5, 10, 20) # 5 pairs with values in the range -20 to 20, except pairs using the range -10 to 10
  29. ]
  30. pprint(result)

Demo : https://trinket.io/python3/f9e65fe9ef

答案3

得分: 1

以下是您要翻译的内容:

  1. 我可以这样做使用一个数字列表来获取sample”,而不必多余地删除零使用一个字典来强制唯一性使用集合会更简单但字典会保留插入顺序如果您希望最终列表中的不同样本的排列方式相同并且较小的数字位于前面这很重要):
  2. ```python
  3. import random
  4. max_num = 150
  5. nums = list(range(-max_num, max_num))
  6. nums.remove(0)
  7. my_list = list({
  8. tuple(
  9. random.sample(nums[max_num-r:r-max_num or None], 2)
  10. ): 0
  11. for c, r in ((5, 5), (75, 100), (20, 150))
  12. for _ in range(c)
  13. })

生成一个单独的nums列表比为每个样本群体生成一个新列表稍微复杂一些,但效率更高,因为我们不需要反复重新分配它,只需根据需要取片段。

请注意,max_num需要至少与for c, r 迭代中所有r值一样大,否则我们将无法通过使用max_num-r等来切片nums来获得所需的范围。

  1. <details>
  2. <summary>英文:</summary>
  3. I might do it like this, using a list of numbers to be able to take the `sample` without having to take an extra step of dropping the zeroes, and using a dict to enforce uniqueness (a set would be simpler, but a dict preserves insertion order, which is important if you want the same arrangement of the different samples in the final list, with smaller numbers toward the beginning):

import random

max_num = 150
nums = list(range(-max_num, max_num))
nums.remove(0)

my_list = list({
tuple(
random.sample(nums[max_num-r:r-max_num or None], 2)
): 0
for c, r in ((5, 5), (75, 100), (20, 150))
for _ in range(c)
})

  1. Generating a single `nums` list is a little more complicated than generating a new list for each sample population, but is more efficient since then we don&#39;t need to repeatedly re-allocate it, and can simply take slices as needed.
  2. Note that `max_num` needs to be at least as big as all of the `r` values in the `for c, r` iteration, or we won&#39;t be able to get the desired range by slicing `nums` with `max_num-r` etc.
  3. </details>

huangapple
  • 本文由 发表于 2023年3月7日 05:36:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定