Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple random generators generate the same values #1734

Closed
rick-de-water opened this issue Aug 23, 2019 · 3 comments
Closed

Multiple random generators generate the same values #1734

rick-de-water opened this issue Aug 23, 2019 · 3 comments

Comments

@rick-de-water
Copy link
Contributor

rick-de-water commented Aug 23, 2019

Describe the bug
When using multiple random generators, they use the same seed and thus generate the same values.

Expected behavior
Each random generator uses its own seed and produces different numbers

Reproduction steps

uint8_t a = GENERATE(take(10, random(0, 255)));
uint8_t b = GENERATE(take(10, random(0, 255)));

a and b will always have the same value

Platform information:

  • Catch version: v2.9.2
@rick-de-water rick-de-water changed the title Multiple random generators produce the same results Multiple random generators generate the same values Aug 23, 2019
@amitherman95
Copy link
Contributor

I tried to reproduce the bug:
Platform: Windows 10
Compiler: MinGW32
Catch version: 2.9.2

Code:

TEST_CASE("bugTest","[generators]"){
uint8_t a = GENERATE(take(5, random(0, 255)));
uint8_t b = GENERATE(take(5, random(0, 255)));
std::cout << "a=" <<(int)a <<" b="<<(int)b<<std::endl;
}

Output:

a=0 b=0
a=0 b=21
a=0 b=153
a=0 b=228
a=0 b=247
a=21 b=0
a=21 b=21
a=21 b=153
a=21 b=228
a=21 b=247
a=153 b=0
a=153 b=21
a=153 b=153
a=153 b=228
a=153 b=247
a=228 b=0
a=228 b=21
a=228 b=153
a=228 b=228
a=228 b=247
a=247 b=0
a=247 b=21
a=247 b=153
a=247 b=228
a=247 b=247

random() uses the same set over and over
I'll look into that

@horenmar
Copy link
Member

horenmar commented Sep 6, 2019

Yeah, this is in many ways an intended behaviour -- the idea was to guarantee reproducible results given seed, so that the user can rerun failing test and examine what happened in greater detail.

I see that the linked PR centralizes handling of random generators, I'll have to think about it a bit.

@amitherman95
Copy link
Contributor

amitherman95 commented Sep 6, 2019

Martin, the numbers are still reproducible, but the random generator(m_rand) is kept somewhere else, to avoid reinitialization when a GeneratorTracker decides to clear its child tracker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants