How do I create a list of random numbers without d

2019-01-03 05:48发布

I tried using random.randint(0, 100), but some numbers were the same. Is there a method/module to create a list unique random numbers?

def getScores():
    # open files to read and write
    f1 = open("page.txt", "r");
    p1 = open("pgRes.txt", "a");

    gScores = [];
    bScores = [];
    yScores = [];

    # run 50 tests of 40 random queries to implement "bootstrapping" method 
    for i in range(50):
        # get 40 random queries from the 50
        lines = random.sample(f1.readlines(), 40);

标签: python random
14条回答
smile是对你的礼貌
2楼-- · 2019-01-03 06:48

If you wish to ensure that the numbers being added are unique, you could use a Set object

if using 2.7 or greater, or import the sets module if not.

As others have mentioned, this means the numbers are not truly random.

查看更多
爷的心禁止访问
3楼-- · 2019-01-03 06:49

If the list of N numbers from 1 to N is randomly generated, then yes, there is a possibility that some numbers may be repeated.

If you want a list of numbers from 1 to N in a random order, fill an array with integers from 1 to N, and then use a Fisher-Yates shuffle or Python's random.shuffle().

查看更多
登录 后发表回答