Home  >  Q&A  >  body text

随机数 - 有没有用Python生成n个不重复随机坐标的算法?

有没有用Python生成n个不重复随机坐标的算法?比如范围(5,3)内的坐标:
1 2
1 3
2 2
2 3
等等

迷茫迷茫2740 days ago958

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:25:28

    python 3

    import itertools
    list(itertools.product(range(1, 6), range(1, 4)))

    If randomization is needed, just randomly generate the index in the above list

    import random
    n = 5
    random_list = list(itertools.product(range(1, 6), range(1, 4)))
    
    random.sample(random_list, n)

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:25:28

    import random
    
    n = 5
    for x in random.sample(range(3 * 5), n):
        print('({}, {})'.format(*pmod(x, 3)))

    reply
    0
  • Cancelreply