Implement truth probability for boolean generator #1793
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What was wrong
Currently, it's impossible to generate booleans that are biased towards
True
orFalse
values.How this fixes it
This patch adds an optional
truth_probability
argument to thepybool()
method which allows to generate booleans that are biased towardsTrue
orFalse
values.Having the new argument is optional and defaults to
50%
, this patch is fully backward compatible.Usage example
Imagine we need to generate random booleans but we only want 10% of them to be equal to
True
. After this patch it would be possible using the following code:Side notes
Our PHP friends already have this feature implemented which proves that it is actually useful for the end-users.
PHP implementation has
chanceOfGettingTrue
as the name of the argument, but I believe thattruth_probability
would fit better.Seems like it's impossible to use
parametrize
in that test case because it extendsunittest.TestCase
, that's why I added a helper method to the test class.