{"id":"randomness-randint","slug":"randint","title":"Random whole numbers","text":"Games, simulations and quizzes need numbers nobody can predict: the roll of a die, the next card, the order of the questions. The standard library's **`random`** module makes them.\n\n### randint\n\n`random.randint(a, b)` gives a random whole number from `a` to `b`. This program rolls a die five times:\n\n```python\nimport random\n\nrandom.seed(2026)\nfor i in range(5):\n    print(\"You rolled\", random.randint(1, 6))\n```\n\n```output\nYou rolled 1\nYou rolled 3\nYou rolled 5\nYou rolled 5\nYou rolled 6\n```\n\nWhat about the line `random.seed(2026)`? It makes the program pick the same \"random\" numbers on every run, so the output shown here is what you'll see when you run it too. Every example in these lessons that uses random numbers starts with such a line, and step 3 of this module explains how it works. Leave it out, and the numbers change on every run, which is what a real game wants.\n\n### Both ends included\n\nUnlike `range(1, 6)`, which stops before 6, `randint(1, 6)` can give 1 and 6 and everything between. Roll a thousand times and both ends turn up:\n\n```python\nimport random\n\nrandom.seed(1)\nrolls = []\nfor i in range(1000):\n    rolls.append(random.randint(1, 6))\nprint(\"Smallest:\", min(rolls))\nprint(\"Largest:\", max(rolls))\n```\n\n```output\nSmallest: 1\nLargest: 6\n```\n\n### Using the number\n\nA random number is an ordinary integer, so it works in calculations and conditions like any other. Here `randint(0, 1)` tosses a coin, and the functions come in with `from`, as in the previous module:\n\n```python\nfrom random import randint, seed\n\nseed(7)\nfor i in range(4):\n    if randint(0, 1) == 0:\n        print(\"Heads\")\n    else:\n        print(\"Tails\")\n```\n\n```output\nTails\nHeads\nTails\nHeads\n```\n\n### One call, one number\n\nEvery call of `randint` gives a new number, but a variable keeps the one it was given. This program rolls the die once and prints that same roll three times:\n\n```python\nimport random\n\nrandom.seed(3)\nroll = random.randint(1, 6)\nfor i in range(3):\n    print(roll)\n```\n\n```output\n2\n2\n2\n```\n\nFor three different rolls, call `random.randint(1, 6)` inside the loop, once for each roll.\n","task":"Fill in the blanks so the program rolls a 20-sided die (the numbers 1 to 20) three times.\n","kind":"fill","seed":"import random\n\nrandom.seed(20)\nfor i in range(3):\n    print(random.[[randint]](1, [[20]]))\n","solution":"import random\n\nrandom.seed(20)\nfor i in range(3):\n    print(random.randint(1, 20))\n","hints":["The function that gives a random whole number between two numbers is the one this step is about.","Type `randint` into the first blank. Both ends are included, so the largest number is the second argument: 20."],"tests":[{"name":"Output","stdin":"","expect":"5\n9\n4","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Write a function `roll_dice(count)` that rolls a six-sided die `count` times with `randint` and returns the rolls in a list, in the order they were rolled.\n\nThe checks set the seed themselves, so leave `random.seed` out of the function.\n","kind":"code","seed":"import random\n\n\ndef roll_dice(count):\n    pass\n\n\nif __name__ == \"__main__\":\n    print(roll_dice(5))\n","solution":"import random\n\n\ndef roll_dice(count):\n    rolls = []\n    for i in range(count):\n        rolls.append(random.randint(1, 6))\n    return rolls\n\n\nif __name__ == \"__main__\":\n    print(roll_dice(5))\n","hints":["Start with an empty list, and loop `count` times.","Each time round, append a new roll: `rolls.append(random.randint(1, 6))`. Return the list after the loop."],"tests":[{"name":"Test 1","stdin":"","call":"import random\nrandom.seed(5)\nprint(roll_dice(6))\n","expect":"[5, 3, 6, 3, 6, 6]","hidden":false},{"name":"print(len(roll_dice(10)))","stdin":"","call":"print(len(roll_dice(10)))\n","expect":"10","hidden":false},{"name":"Test 3","stdin":"","call":"import random\nrandom.seed(8)\nrolls = roll_dice(500)\nprint(min(rolls), max(rolls))\n","expect":"1 6","hidden":true},{"name":"roll_dice(0)","stdin":"","call":"roll_dice(0)\n","expect":"[]","hidden":true,"returns":true},{"name":"Test 5","stdin":"","call":"import random\nrandom.seed(13)\nprint(roll_dice(3))\n","expect":"[3, 3, 6]","hidden":true}],"require":[],"forbid":[{"pattern":"\\bseed\\s*\\(","message":"Leave `random.seed` out of the function; the checks set the seed before they call it."}]},{"task":"Write a number guessing game. The program first asks for a game number and passes it to `random.seed`, so the same game number always gives the same secret number (handy for testing, and for challenging a friend to the very same game). Then it picks the secret number with `random.randint(1, 100)` and asks for guesses until the player finds it. It answers each wrong guess with `Too small` or `Too big`, and the right one with `Correct!` and the number of guesses. For example:\n\n```\nGame number: 1\nGuess: 50\nToo big\nGuess: 25\nToo big\nGuess: 12\nToo small\nGuess: 18\nCorrect! Guesses: 4\n```\n","kind":"code","seed":"import random\n\ngame = int(input(\"Game number: \"))\nrandom.seed(game)\n","solution":"import random\n\ngame = int(input(\"Game number: \"))\nrandom.seed(game)\nsecret = random.randint(1, 100)\nguesses = 0\nwhile True:\n    guess = int(input(\"Guess: \"))\n    guesses += 1\n    if guess < secret:\n        print(\"Too small\")\n    elif guess > secret:\n        print(\"Too big\")\n    else:\n        print(\"Correct! Guesses:\", guesses)\n        break\n","hints":["Pick the secret number once, right after the seed: `secret = random.randint(1, 100)`. Picking it inside the loop would change it after every guess.","Use a `while True` loop that reads a guess and adds one to a counter each time round.","Compare the guess with the secret number. When they're equal, print the message with the counter and leave the loop with `break`."],"tests":[{"name":"Test 1","stdin":"1\n50\n25\n12\n18\n","expect":"Game number: 1\nGuess: 50\nToo big\nGuess: 25\nToo big\nGuess: 12\nToo small\nGuess: 18\nCorrect! Guesses: 4","hidden":false},{"name":"Test 2","stdin":"7\n42\n","expect":"Game number: 7\nGuess: 42\nCorrect! Guesses: 1","hidden":true},{"name":"Test 3","stdin":"12\n50\n75\n60\n61\n","expect":"Game number: 12\nGuess: 50\nToo small\nGuess: 75\nToo big\nGuess: 60\nToo small\nGuess: 61\nCorrect! Guesses: 4","hidden":true}],"require":[],"forbid":[]}]}