Seed method for testing purpose

Issue #40 closed
Former user created an issue

Hi, I wish to thank you for your wonderful work, I'm learning a lot with Lea, and I think this will help me a lot studiyng statistics. Do you thinks it's possible to add a seed(x) method to force Lea to work with the same random response when calling RandomDraw, for testing purposes? I tried to modify _createRandomIter function importing random (and then using random.randrange method), but I can't figure out why, I get "AttributeError: 'builtin_function_or_method' object has no attribute 'seed'. It seems a subclassing issue, but I don't know python so well as I thought, I'm afraid.
I'm using 2.3.5 version Many thanks in advance Giordano

Comments (7)

  1. Pierre Denis repo owner

    Thank you for your nice comments, Giordano! I’m glad that Lea helps you.

    Actually, there is a simple solution: use the standard Python’s random.seed(…) function. Lea internally uses Python random module, so it will be impacted. So, in your session/script using Lea, you add

    import random
    

    Then before calling Lea’s .random(…) or .randomDraw(…), you call

    random.seed(x)
    

    You will then get the very same random sample whenever you seed with x.

    Therefore, you can achieve this without patching Lea (which is always a dangerous business!). BTW, I had the same idea as you in the past, that is to add a Lea.seed(x) method that simply calls random.seed(x). I’ve abandoned this idea because 1) there’s poor added value in this convenience method and 2) calling Lea.seed(x) may give you the impression that you do something internal to Lea although it may impact any module that is currently using the Python random module. To avoid such side-effect, I judge that it is safer to let the user make an explicit call to random.seed(x).

    Hoping this help you,

  2. Giordano Ducci

    Hi Pierre, many thanks for your kind and very fast reply! I tested your solution right now and it woks very well! The simplest solution is often the better, isn't it!? :) You're right, it's not a good idea to patch a library, but it's been a very useful way to understand your code; and you're right not adding a Lea.seed(x) too, it could lead to a weird impression, expecially if you "forget" that you called it! Many thanks again, please keep on working with Lea, it's a very powerful tool! Giordano

  3. Pierre Denis repo owner

    Since your question is of interest for everybody, I've just updated the wiki pages (Lea v2 & v3) to explain that technique with a small example - see end of section on random sampling.

  4. Log in to comment