random-data-generator icon indicating copy to clipboard operation
random-data-generator copied to clipboard

Consecutive calls of `random` returns the same instance

Open zzvara opened this issue 8 years ago • 9 comments

Consecutive calls of random returns the same instance. Providing an integer to get multiple random elements will however generate different instances. How to overcome this? (I'm not familiar with Shapless, just want to have simple instances fast.)

zzvara avatar Jun 24 '17 12:06 zzvara

Hi @zzvara, this is actually not a Shapeless problem, but it is enforced by design of the library.

See https://github.com/DanielaSfregola/random-data-generator/pull/37#issuecomment-293801647 for an explanation of this issue.

Happy to discuss possible solutions or suggestions on how to improve this, if you have any.

DanielaSfregola avatar Jun 26 '17 17:06 DanielaSfregola

I see, thanks for clarifying. I'll modify my test design according to this.

My idea would be to specify an another seed, a "transition seed", in which consecutive "picks" from the random[T] function with the same type would generate a new T as follows. Combine the (RANDOM_DATA_GENERATOR_SEED: Seed, NTH_CALL_FOR_T: Int, TRANSITION_SEED: Seed) to derive a new RANDOM_DATA_GENERATOR_SEED for that random generation. Still, "most" of the tests could be replicated.

On 2017-06-26 19:08:11, Daniela Sfregola [email protected] wrote: Hi @zzvara [https://github.com/zzvara], this is actually not a Shapeless problem, but it is enforced by design of the library. See #37 (comment) [https://github.com/DanielaSfregola/random-data-generator/pull/37#issuecomment-293801647] for an explanation of this issue. Happy to discuss possible solutions or suggestions on how to improve this, if you have any. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub [https://github.com/DanielaSfregola/random-data-generator/issues/44#issuecomment-311121631], or mute the thread [https://github.com/notifications/unsubscribe-auth/AH--TVk-THCy0oLnR4ndPiiIitDL-nynks5sH-V4gaJpZM4OEUSf].

zzvara avatar Jun 26 '17 17:06 zzvara

How would you handle that tests can run in parallel? i.e.: the order in which random is called is not guaranteed...

DanielaSfregola avatar Jun 26 '17 18:06 DanielaSfregola

Encapsulate random into an Iterator, and each thread should acquire the iterator instead of accessing random directly. Iterator only needs to store NTH_CALL_FOR_T.

On 2017-06-26 20:07:19, Daniela Sfregola [email protected] wrote: How would you handle that tests can run in parallel? i.e.: the order in which random is called is not guaranteed... — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub [https://github.com/DanielaSfregola/random-data-generator/issues/44#issuecomment-311137009], or mute the thread [https://github.com/notifications/unsubscribe-auth/AH--TcNjvCEffZ1S5xapvP6TWPppIjPlks5sH_NWgaJpZM4OEUSf].

zzvara avatar Jun 27 '17 05:06 zzvara

Give it a try @zzvara! Please, if it works I'd love to see a PR on this.

Cheers, D.

DanielaSfregola avatar Jun 27 '17 06:06 DanielaSfregola

Hi @zzvara and @DanielaSfregola Why can't we go for "call by name" which makes the body of the method to execute, every time when this method is called? This makes every, consecutive call to random an actual invocation and will always return a new instance (instead of cached results). I am pretty sure that, I am missing some context. If so, please correct me. Thank you.

krishna-meduri avatar Mar 19 '19 08:03 krishna-meduri

Hi @krishna-meduri, using simply random values will not work: we want the ability to reproduce a "randomly generated session", so that people can debug and fix their tests when a problematic session is found.

In the current implementation, requesting a random value of the same type will provide always the same value because we fix the seed.

I am pretty sure there must be a solution - such as having the idea of the global seed and a local seed that we can combine to obtain different values of the same type.

We "just" need to find a solution that guarantees that a session can be reproduced by fixing the seed independently from the execution order of a test.

DanielaSfregola avatar Mar 20 '19 08:03 DanielaSfregola

@DanielaSfregola Got it. Thank you. Will try to see if I could come up with a solution and will update you.

krishna-meduri avatar Mar 21 '19 06:03 krishna-meduri

I just use these 2 directly without random-data-generator:

import org.scalacheck.Arbitrary
import org.scalacheck.Gen

https://booksites.artima.com/scalacheck/examples/html/ch06.html

anton-zen avatar Mar 22 '22 07:03 anton-zen