[Python] random.sample() generating same sequence every time it is run

Discussão em 'Python' iniciado por Stack, Janeiro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I run this code on two machines

    from apscheduler.schedulers.asyncio import AsyncIOScheduler

    # this part is simplified. It is only here to show how scheduler is basically initialized (for context)
    scheduler = AsyncIOScheduler(timezone=utc)
    scheduler.start()

    # This is real code (with exception of the list)
    @scheduler.scheduled_job('interval', minutes=1, misfire_grace_time=None)
    async def do_dada_news():
    pages = [...] # shortened for better readability. It is longer than 20 elements
    print("---")
    for page in random.sample(pages, min(len(pages), 20)):
    print(page)


    on both machines I get output. Which is strange:

    • on machine a) I get 20 different lines every time do_dada_news() is run. This code is run in a container in docker locally
    • on machine b) I get the exact same 20 lines every time it is run. This machhine is a container in a kubernetes cluser

    I expect both machines to have the same behavior like machine a). How can this be such a different behavior?

    PS: To temporarily fix the problem I now do random.seed(time.time()*10000) inside do_dada_news(). But that does not feel right

    Continue reading...

Compartilhe esta Página