Skip to content

Instantly share code, notes, and snippets.

@AbstractBeliefs
Created March 28, 2018 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbstractBeliefs/af513a07c981c01cd2b5d7be167bd8fc to your computer and use it in GitHub Desktop.
Save AbstractBeliefs/af513a07c981c01cd2b5d7be167bd8fc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[86, 37, 33, 21, 35]\n",
"[12]\n",
"[17, 32, 35]\n"
]
}
],
"source": [
"# make a function that will get a sequence of n random numbers\n",
"def get_n_random(n):\n",
" return [random.randint(1,100) for _ in range(n)]\n",
"\n",
"print get_n_random(5)\n",
"print get_n_random(1)\n",
"print get_n_random(3)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First ten numbers are:\n",
"[14, 85, 77, 26, 50, 45, 66, 79, 10, 3]\n",
"Demonstrate we can restart the order of random numbers with seed\n",
"[14, 85, 77, 26, 50, 45, 66, 79, 10, 3]\n"
]
},
{
"ename": "AssertionError",
"evalue": "15 sequence didn't match",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-c33acaf43656>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mget_n_random\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m15\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mtest_sequence\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m15\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"15 sequence didn't match\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mtest_random\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-4-c33acaf43656>\u001b[0m in \u001b[0;36mtest_random\u001b[0;34m()\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;31m# Forget to see this time\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mget_n_random\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m15\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mtest_sequence\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m15\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"15 sequence didn't match\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 19\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0mtest_random\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAssertionError\u001b[0m: 15 sequence didn't match"
]
}
],
"source": [
"# To test this, we'll make a function that gets a fixed random sequence, and ensure that future calls match it\n",
"def test_random():\n",
" random.seed(1)\n",
" test_sequence = get_n_random(50)\n",
" print \"First ten numbers are:\\n%s\" % test_sequence[:10]\n",
" \n",
" print \"Demonstrate we can restart the order of random numbers with seed\"\n",
" random.seed(1)\n",
" print get_n_random(10)\n",
" \n",
" random.seed(1)\n",
" assert get_n_random(10) == test_sequence[:10], \"10 sequence didn't match\"\n",
" \n",
" random.seed(1)\n",
" assert get_n_random(4) == test_sequence[:4], \"10 sequence didn't match\"\n",
" \n",
" # Forget to see this time\n",
" assert get_n_random(15) == test_sequence[:15], \"15 sequence didn't match\"\n",
"\n",
"test_random()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment