random.randint shows different output in Python 2.x and Python 3.x with same seed

后端 未结 3 1325
旧时难觅i
旧时难觅i 2021-02-14 20:22

I am porting the application from python 2 to python 3 and encountered the following problem: random.randint returns different result according to used Python versi

3条回答
  •  长情又很酷
    2021-02-14 20:43

    Finally found the answer!

    Sparky05 give interesting idea and was near with int(1+99*random.random()).

    But the right answer is

    random.seed(seed, version=1)
    int(random.random() * 100) + 1
    

    in Python 3.x

    Works in the same way like

    random.seed(seed)
    random.randint(1, 100)
    

    in Python 2.x

提交回复
热议问题