dimanche 28 mars 2021

Python seed (random.seed) is not giving replicable results

I want to create some code that uses both random.sample and numpy.random.randint but in such a way that is completely replicable. How do I properly set the seed for random.sample?

import numpy as np
import pandas as pd
import random

np.random.seed(3)
random.seed(3)

numteam = 3

nameslist = {"Abigail", "Briana", "Charlotte", "Daisy", "Emma", "Florence", "Gail", "Heather", "Imogen", "Jane", "Kelly", "Mara", "Natalie", "Olivia", "Patricia"}
team = random.sample(nameslist, numteam)

df = pd.DataFrame(np.random.randint(0,10,size=(numteam, numteam)), columns = team)
df.index = team

print(df)

I ran this code twice on the same online compiler and get different name selections (the randint is consistent) enter image description here

Interestingly, if I use a different online compiler the seed seems to stick. I need to be able to have others run the code using whatever compiler they like and get the exact same "random" data frame as what I have (each user will have a unique seed so no two people get the same data frame).

I've looked at a lot of examples online, but from what I can tell random.seed is what I am meant to be doing. I am using Pycharm 2020.2 with Python 3.8 locally. The two online compilers I have tried are https://www.tutorialspoint.com/execute_python_online.php and https://pynative.com/online-python-code-editor-to-execute-python-code/




Aucun commentaire:

Enregistrer un commentaire