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

后端 未结 3 1004
余生分开走
余生分开走 2021-02-14 20:38

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:56

    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

提交回复
热议问题