Home > database >  A random seed that makes tests fail the same way every time?
A random seed that makes tests fail the same way every time?

Time:10-17

I've never worked with the random library and I'm a bit confused about what this fixme statement is asking me to do. Is there a seed number that specifically does this?

The code is making an ordered list ADT and this file is testing the functions within that file. We use the random library to create random numbers that will be added into, or removed from the ordered list ADT.

Please let me know if there's any other information I should give!

enter image description here

enter image description here

CodePudding user response:

When you set the seed, as is done here, you should get a consistent set of random numbers afterwards.

If the numbers that you're getting are NOT consistent, then one of the following is true:

  1. You're using a different random number function somewhere so setting the seed didn't fix it.
  2. The test depends on external state, so it does different things on different runs.
  3. You're trying to compare running the entire test suite to testing a single function.

The right fix will depend on which of those is the issue.

  1. Find where you're using a different generator and either stop, or mock it out.
  2. Find the external state and set it or mock it out.
  3. Move setting the seed into a setUp method so that it is set fresh for every test.

My guess is that your problem is the last. But if it isn't, hopefully this answer gives you somewhere to start digging from.

CodePudding user response:

The random library will generate the same sequence of random numbers (!) when you provide the same seed value. Any seed will do - 652 is fine as in your example - but could be 7, or 42, or any int value. This is counter-intuitive but basic random numbers generated like this are more accurately called pseudo-random numbers - they are not 100% random since the random sequence can be regenerated exactly the same. This is good for testing though, as indicated in your question.

For this parameter we have the following notes from the python docs:

random.seed(a=None)

  • Initialize the random number generator.

  • If a is omitted or None, the current system time is used. If randomness sources are provided by the operating system, they are used instead of the system time.

  • If a is an int, it is used directly.

CodePudding user response:

When you fix a number using the seed method, all calls for a pseudo-random methods will return the same numbers associated to order of the calls. If you change the number you will have different succession of random numbers. That's why if you want that the random calls produce a different or not controlled value set the seed with a hard to predict number like the exact current time.

  • Related