Testing with random numbers in Python

Continuation of Scientific Computing with Python

I'm studying Python steadily at freeCodeCamp. In Previous article, this time we will challenge * Probability Calculator *.

Question 5: Probability Calculator

This problem is a problem that seems to have been solved as a probability problem when I was a student. Find the probability of taking out multiple colored balls in a hat (bag). In particular,

--Creating a Hat class --Creating an experiment method: A method for finding the probability

Personal point: Testing with random numbers

This time, there is a process of randomly taking out the ball from the hat. It doesn't work when I want to write a test like below.

 1 import unittest
 2 
 3 class UnitTests(unittest.TestCase):
 4     #When you take out 2 balls from a hat with 5 red balls and 2 blue balls, there is 1 red ball and 1 blue ball.
 5     def test_hat_draw(self):
 6        hat = prob_calculator.Hat(red=5,blue=2)
 7        actual = hat.draw(2)
 8        expected = ['blue', 'red']
 9        self.assertEqual(actual, expected, 'Expected hat draw to return two random items from hat contents.')
10        actual = len(hat.contents)
11        expected = 5
12        self.assertEqual(actual, expected, 'Expected hat draw to reduce number of items in contents.')

When you draw two balls on the 7th line, the internal implementation uses random numbers, so there is not always one red ball and one blue ball. Use `random seed`` to solve this.


import random

balls = ['red', 'red', 'red', 'red', 'red', 'blue', 'blue']

random.sample(balls, k=2)
# ['red', 'red']
random.sample(balls, k=2)
# ['red', 'blue']

random.seed(0)
random.sample(balls, k=2)
# ['blue', 'red']
random.seed(0)
random.sample(balls, k=2)
# ['blue', 'red']

By setting the seed of random numbers in advance in this way, the same behavior can always be achieved.

Finally

Scientific Computing with Python is over! Next, I'm going to start doing Data Analysis with Python Certification!

Recommended Posts

Testing with random numbers in Python
Random walk in Python
Prime numbers in Python
Testing methods that return random values in Python
Scraping with selenium in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Automate python testing with CircleCI
Determine prime numbers with python
Working with sounds in Python
Scraping with Selenium in Python
Use Random Forest in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Weighted random choice in python
Handle complex numbers in Python
[Python] Get the numbers in the graph image with OCR
Number recognition in images with Python
Playing handwritten numbers with python Part 1
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
[Small story] [Python] Replace strings in 2D arrays with numbers
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
random French number generator with python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Law of large numbers in python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Create a random string in Python
Get started with Python in Blender
Working with DICOM images in Python
Write documentation in Sphinx with Python Livereload
Get additional data in LDAP with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Try logging in to qiita with Python
Stress Test with Locust written in Python
Exclusive control with lock file in Python
Device monitoring with On-box Python in IOS-XE
Play handwritten numbers with python Part 2 (identify)
[Python] How to display random numbers (random module)
Try working with binary data in Python
Draw Nozomi Sasaki in Excel with python
Tips for dealing with binaries in Python
Collectively implement statistical hypothesis testing in Python
Display Python 3 in the browser with MAMP
Page cache in Python + Flask with Flask-Caching
Algorithm learned with Python 4th: Prime numbers
Post Test 3 (Working with PosgreSQL in Python)
How to work with BigQuery in Python
Playing card class in Python (with comparison)
Dealing with "years and months" in Python
Process multiple lists with for in Python